/**
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
 * CANN Open Software License Agreement Version 2.0 (the "License").
 * Please refer to the License for details. You may not use this file except in compliance with the License.
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
 * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
 * See LICENSE in the root of the software repository for the full text of the License.
 */

#define _GNU_SOURCE
#include <stdlib.h>
#include <errno.h>
#include <sys/prctl.h>
#include "securec.h"
#include "user_log.h"
#include "dl_hal_function.h"
#include "ra_comm.h"
#include "ra_hdc.h"
#include "ra_hdc_async.h"
#include "ra_hdc_lite.h"
#include "ra_hdc_rdma_notify.h"
#include "ra_hdc_rdma.h"
#include "ra_hdc_socket.h"
#include "ra_rs_comm.h"
#include "ra_hdc_tlv.h"
#include "ra_rs_err.h"
#include "rs.h"
#include "rs_ping.h"
#ifdef CONFIG_TLV
#include "ra_adp_tlv.h"
#endif
#include "ra_adp_async.h"
#include "ra_adp.h"

struct RaHdcServer gHdcServer[RA_MAX_PHY_ID_NUM] = {0};
struct RaHdcInitPara gHdcInitPara = {0};
struct RsPthreadInfo gRaThreadInfo = {0};

struct RsOps {
    int (*socketBatchConnect)(struct SocketConnectInfo conn[], unsigned int num);
    int (*socketBatchClose)(int disuseLinger, struct RsSocketCloseInfoT conn[], unsigned int num);
    int (*socketBatchAbort)(struct SocketConnectInfo conn[], unsigned int num);
    int (*socketListenStart)(struct SocketListenInfo conn[], unsigned int num);
    int (*socketListenStop)(struct SocketListenInfo conn[], unsigned int num);
    int (*getSockets)(unsigned int role, struct SocketFdData conn[], unsigned int num);
    int (*socketSend)(int fd, const void *data, uint64_t size);
    int (*socketRecv)(int fd, void *data, uint64_t size);
    int (*socketInit)(const unsigned int *vnicIp, unsigned int num);
    int (*socketDeinit)(struct rdev rdevInfo);
    int (*rdevInit)(struct rdev rdevInfo, unsigned int notifyType, unsigned int *rdevIndex);
    int (*rdevInitWithBackup)(struct rdev rdevInfo, struct rdev backupRdevInfo,
        unsigned int notifyType, unsigned int *rdevIndex);
    int (*rdevGetPortStatus)(unsigned int phyId, unsigned int rdevIndex, enum PortStatus *status);
    int (*rdevDeinit)(unsigned int phyId, unsigned int notifyType, unsigned int rdevIndex);
    int (*qpCreate)(unsigned int phyId, unsigned int rdevIndex, struct RsQpNorm qpNorm,
        struct RsQpResp *qpResp);
    int (*qpCreateWithAttrs)(unsigned int phyId, unsigned int rdevIndex,
        struct RsQpNormWithAttrs *qpNorm, struct RsQpRespWithAttrs *qpResp);
    int (*qpDestroy)(unsigned int phyId, unsigned int rdevIndex, unsigned int qpn);
    int (*typicalQpModify)(unsigned int phyId, unsigned int rdevIndex, struct TypicalQp localQpInfo,
        struct TypicalQp remoteQpInfo, unsigned int *udpSport);
    int (*qpBatchModify)(unsigned int phyId, unsigned int rdevIndex, int status, int qpn[], int qpnNum);
    int (*qpConnectAsync)(unsigned int phyId, unsigned int rdevIndex, unsigned int qpn, int fd);
    int (*getQpStatus)(unsigned int phyId, unsigned int rdevIndex, unsigned int qpn,
        struct RsQpStatusInfo *qpInfo);
    int (*mrReg)(unsigned int phyId, unsigned int rdevIndex, unsigned int qpn, struct RdmaMrRegInfo *mrRegInfo);
    int (*mrDereg)(unsigned int phyId, unsigned int rdevIndex, unsigned int qpn, char *addr);
    int (*registerMr)(unsigned int phyId, unsigned int rdevIndex, struct RdmaMrRegInfo *mrRegInfo,
        void **mrHandle);
    int (*typicalRegisterMr)(unsigned int phyId, unsigned int rdevIndex, struct RdmaMrRegInfo *mrRegInfo,
        void **mrHandle);
    int (*remapMr)(unsigned int phyId, unsigned int rdevIndex, struct MemRemapInfo memList[],
        unsigned int memNum);
    int (*typicalDeregisterMr)(unsigned int phyId, unsigned int devIndex, unsigned long long addr);
    int (*sendWr)(unsigned int phyId, unsigned int rdevIndex, unsigned int qpn, struct SendWr *wr,
        struct SendWrRsp *wrRsp);
    int (*sendWrList)(struct RsWrlistBaseInfo baseInfo, struct WrInfo *wrList,
        unsigned int sendNum, struct SendWrRsp *wrRsp, unsigned int *completeNum);
    int (*getNotifyMrInfo)(unsigned int phyId, unsigned int rdevIndex, struct MrInfoT *info);
    int (*notifyCfgSet)(unsigned int phyId, unsigned long long va, unsigned long long size);
    int (*notifyCfgGet)(unsigned int phyId, unsigned long long *va, unsigned long long *size);
    int (*setHostPid)(unsigned int phyId, pid_t hostPid, const char *pidSign);
    int (*whiteListAdd)(struct rdev rdevInfo, struct SocketWlistInfoT whiteList[], unsigned int num);
    int (*whiteListDel)(struct rdev rdevInfo, struct SocketWlistInfoT whiteList[], unsigned int num);
    int (*acceptCreditAdd)(struct SocketListenInfo conn[], uint32_t num, unsigned int creditLimit);
    int (*getIfnum)(unsigned int phyId, bool isAll, unsigned int *num);
    int (*getIfaddrs)(struct IfaddrInfo ifaddrInfos[], unsigned int *num, unsigned int phyId);
    int (*getIfaddrsV2)(struct InterfaceInfo interfaceInfos[], unsigned int *num, unsigned int phyId,
        bool isAll);
    int (*getVnicIp)(unsigned int phyId, unsigned int *vnicIp);
    int (*getVnicIpInfos)(unsigned int phyId, enum IdType type, unsigned int ids[], unsigned int num,
        struct IpInfo infos[]);
    int (*getInterfaceVersion)(unsigned int opcode, unsigned int *version);
    int (*setTsqpDepth)(unsigned int phyId, unsigned int rdevIndex, unsigned int tempDepth, unsigned int *qpNum);
    int (*getTsqpDepth)(unsigned int phyId, unsigned int rdevIndex, unsigned int *tempDepth, unsigned int *qpNum);
    int (*setQpAttrQos)(unsigned int phyId, unsigned int rdevIndex, unsigned int qpn, struct QosAttr *attr);
    int (*setQpAttrTimeout)(unsigned int phyId, unsigned int rdevIndex, unsigned int qpn, unsigned int *timeout);
    int (*setQpAttrRetryCnt)(unsigned int phyId, unsigned int rdevIndex, unsigned int qpn,
        unsigned int *retryCnt);
    int (*getCqeErrInfo)(struct CqeErrInfo *info);
    int (*getLiteSupport)(unsigned int phyId, unsigned int rdevIndex, int *supportLite);
    int (*getLiteRdevCap)(unsigned int phyId, unsigned int rdevIndex, struct LiteRdevCapResp *resp);
    int (*getLiteQpCqAttr)(
        unsigned int phyId, unsigned int rdevIndex, unsigned int qpn, struct LiteQpCqAttrResp *resp);
    int (*getLiteMemAttr)(
        unsigned int phyId, unsigned int rdevIndex, unsigned int qpn, struct LiteMemAttrResp *resp);
    int (*getLiteConnectedInfo)(
        unsigned int phyId, unsigned int rdevIndex, unsigned int qpn, struct LiteConnectedInfoResp *resp);
    int (*pingInit)(struct PingInitAttr *attr, struct PingInitInfo *info, unsigned int *devIndex);
    int (*pingTargetAdd)(struct RaRsDevInfo *rdev, struct PingTargetInfo *target);
    int (*pingTaskStart)(struct RaRsDevInfo *rdev, struct PingTaskAttr *attr);
    int (*pingGetResults)(struct RaRsDevInfo *rdev, struct PingTargetCommInfo target[],
        unsigned int *num, struct PingResultInfo result[]);
    int (*pingTaskStop)(struct RaRsDevInfo *rdev);
    int (*pingTargetDel)(struct RaRsDevInfo *rdev, struct PingTargetCommInfo target[],
        unsigned int *num);
    int (*pingDeinit)(struct RaRsDevInfo *rdev);
    int (*getCqeErrInfoNum)(unsigned int phyId, unsigned int rdevIdx, unsigned int *num);
    int (*getCqeErrInfoList)(unsigned int phyId, unsigned int rdevIdx, struct CqeErrInfo *info,
        unsigned int *num);
    int (*getTlsEnable)(unsigned int phyId, bool *tlsEnable);
    int (*getSecRandom)(int *value);
    int (*getHccnCfg)(unsigned int phyId, enum HccnCfgKey key, char *value, unsigned int *valueLen);
};

struct RsOps gRaRsOps = {
    .socketBatchConnect = RsSocketBatchConnect,
    .socketBatchClose = RsSocketBatchClose,
    .socketBatchAbort = RsSocketBatchAbort,
    .socketListenStart = RsSocketListenStart,
    .socketListenStop = RsSocketListenStop,
    .getSockets = RsGetSockets,
    .socketSend = RsSocketSend,
    .socketRecv = RsSocketRecv,
    .socketInit = RsSocketInit,
    .socketDeinit = RsSocketDeinit,
    .rdevInit = RsRdevInit,
    .rdevInitWithBackup = RsRdevInitWithBackup,
    .rdevGetPortStatus = RsRdevGetPortStatus,
    .rdevDeinit = RsRdevDeinit,
    .qpCreate = RsQpCreate,
    .qpCreateWithAttrs = RsQpCreateWithAttrs,
    .qpDestroy = RsQpDestroy,
    .typicalQpModify = RsTypicalQpModify,
    .qpBatchModify = RsQpBatchModify,
    .qpConnectAsync = RsQpConnectAsync,
    .getQpStatus = RsGetQpStatus,
    .mrReg = RsMrReg,
    .mrDereg = RsMrDereg,
    .registerMr = RsTypicalRegisterMrV1,
    .typicalRegisterMr = RsTypicalRegisterMr,
    .remapMr = RsRemapMr,
    .typicalDeregisterMr = RsTypicalDeregisterMr,
    .sendWr = RsSendWr,
    .sendWrList = RsSendWrlist,
    .getNotifyMrInfo = RsGetNotifyMrInfo,
    .notifyCfgSet = RsNotifyCfgSet,
    .notifyCfgGet = RsNotifyCfgGet,
    .setHostPid = RsSetHostPid,
    .whiteListAdd = RsSocketWhiteListAdd,
    .whiteListDel = RsSocketWhiteListDel,
    .acceptCreditAdd = RsSocketAcceptCreditAdd,
    .getIfnum = RsGetIfnum,
    .getIfaddrs = RsGetIfaddrs,
    .getIfaddrsV2 = RsGetIfaddrsV2,
    .getVnicIp = RsGetVnicIp,
    .getVnicIpInfos = RsGetVnicIpInfos,
    .getInterfaceVersion = RsGetInterfaceVersion,
    .setTsqpDepth = RsSetTsqpDepth,
    .getTsqpDepth = RsGetTsqpDepth,
    .setQpAttrQos = RsSetQpAttrQos,
    .setQpAttrTimeout = RsSetQpAttrTimeout,
    .setQpAttrRetryCnt = RsSetQpAttrRetryCnt,
    .getCqeErrInfo = RsGetCqeErrInfo,
    .getLiteRdevCap = RsGetLiteRdevCap,
    .getLiteQpCqAttr = RsGetLiteQpCqAttr,
    .getLiteConnectedInfo = RsGetLiteConnectedInfo,
    .getLiteMemAttr = RsGetLiteMemAttr,
    .getLiteSupport = RsGetLiteSupport,
    .pingInit = RsPingInit,
    .pingTargetAdd = RsPingTargetAdd,
    .pingTaskStart = RsPingTaskStart,
    .pingGetResults = RsPingGetResults,
    .pingTaskStop = RsPingTaskStop,
    .pingTargetDel = RsPingTargetDel,
    .pingDeinit = RsPingDeinit,
    .getCqeErrInfoNum = RsGetCqeErrInfoNum,
    .getCqeErrInfoList = RsGetCqeErrInfoList,
    .getTlsEnable = RsGetTlsEnable,
    .getSecRandom = RsDrvGetRandomNum,
    .getHccnCfg = RsGetHccnCfg,
};

struct HdcOps gRaHdcOps = {
    .getCapacity = DlDrvHdcGetCapacity,
    .clientCreate = DlDrvHdcClientCreate,
    .clientDestroy = DlDrvHdcClientDestroy,
    .sessionConnect = DlDrvHdcSessionConnect,
    .sessionConnectEx = DlHalHdcSessionConnectEx,
    .serverCreate = DlDrvHdcServerCreate,
    .serverDestroy = DlDrvHdcServerDestroy,
    .sessionAccept = DlDrvHdcSessionAccept,
    .sessionClose = DlDrvHdcSessionClose,
    .freeMsg = DlDrvHdcFreeMsg,
    .reuseMsg = DlDrvHdcReuseMsg,
    .addMsgBuffer = DlDrvHdcAddMsgBuffer,
    .getMsgBuffer = DlDrvHdcGetMsgBuffer,
    .recv = DlHalHdcRecv,
    .send = DlHalHdcSend,
    .allocMsg = DlDrvHdcAllocMsg,
    .setSessionReference = DlDrvHdcSetSessionReference,
};

#define RA_HDC_OPS gRaHdcOps

STATIC void MsgHeadBuildUpHw(char *pSendRcvBuf, struct MsgHead *recvMsgHead, int ret,
    unsigned int msgDataLen)
{
    struct MsgHead *pSendRcvHead = NULL;

    pSendRcvHead = (struct MsgHead *)pSendRcvBuf;
    pSendRcvHead->opcode = recvMsgHead->opcode;
    pSendRcvHead->asyncReqId = recvMsgHead->asyncReqId;
    pSendRcvHead->ret = ret;
    pSendRcvHead->msgDataLen = msgDataLen;

    return;
}

STATIC int OpMsgErr(char **outBuf, struct MsgHead *recvMsgHead, int *outBufLen, int opRight)
{
    unsigned int opcode = recvMsgHead->opcode;
    char *outBufTmp = NULL;
    int msgRet = 0;

    outBufTmp = (char *)calloc(sizeof(struct MsgHead), sizeof(char));
    CHK_PRT_RETURN(outBufTmp == NULL, hccp_err("send_buf calloc failed."), -ENOMEM);

    if (opRight == HAVE_OP_RIGHT) {
        if (opcode >= RA_RS_OP_MAX_NUM || ((opcode < RA_RS_HDC_SESSION_CLOSE) && (opcode >= RA_RS_EXTER_OP_MAX_NUM))) {
            msgRet = -EPROTONOSUPPORT;
        } else {
            msgRet = -EPIPE;
        }
    } else if (opRight == TGID_INVALID) {
        msgRet = -EPERM;
    } else {
        msgRet = -EACCES;
    }

    MsgHeadBuildUpHw(outBufTmp, recvMsgHead, msgRet, 0);

    *outBuf = outBufTmp;
    *outBufLen = sizeof(struct MsgHead);

    return 0;
}

STATIC int RaRsSocketBatchConnect(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpSocketConnectData *socketConnectData =
        (union OpSocketConnectData *)(inBuf + sizeof(struct MsgHead));
    unsigned int usePort = 0;
    unsigned int i;

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpSocketConnectData), sizeof(struct MsgHead), rcvBufLen,
        opResult);

    // clear resv bit 31 use_port, for compatibility issue
    usePort = socketConnectData->txData.num >> SOCKET_USE_PORT_BIT;
    socketConnectData->txData.num &= ~(1U << SOCKET_USE_PORT_BIT);
    HCCP_CHECK_PARAM_NUM(socketConnectData->txData.num, MAX_SOCKET_NUM);

    for (i = 0; i < (socketConnectData->txData).num; i++) {
        // use_port flag not specify, use default port for compatibility issue
        if (usePort == 0) {
            (socketConnectData->txData).conn[i].port = RS_SOCK_PORT_DEF;
        } else if ((socketConnectData->txData).conn[i].port > MAX_PORT_NUM) {
            hccp_err("[batch_connect]conn[%u].port=%u invalid", i, (socketConnectData->txData).conn[i].port);
            return -EINVAL;
        }
    }

    *opResult = gRaRsOps.socketBatchConnect((socketConnectData->txData).conn,
        (socketConnectData->txData).num);
    if (*opResult != 0) {
        hccp_err("socket batch connect failed ret[%d].", *opResult);
    }

    return 0;
}

STATIC int RaRsSocketBatchClose(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpSocketCloseData *socketCloseData = (union OpSocketCloseData *)(inBuf + sizeof(struct MsgHead));
    int disuseLinger = 0;
    unsigned int i;

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpSocketCloseData), sizeof(struct MsgHead), rcvBufLen, opResult);

    // clear resv bit 31 disuse_linger, for compatibility issue(0 by default)
    disuseLinger = socketCloseData->txData.num >> SOCKET_DISUSE_LINGER_BIT;
    socketCloseData->txData.num &= ~(1U << SOCKET_DISUSE_LINGER_BIT);
    HCCP_CHECK_PARAM_NUM(socketCloseData->txData.num, MAX_SOCKET_NUM);

    struct RsSocketCloseInfoT closeConn[MAX_SOCKET_NUM] = {0};
    for (i = 0; i < socketCloseData->txData.num; i++) {
        closeConn[i].fd = ((socketCloseData->txData).conn[i]).closeFd;
    }
    *opResult = gRaRsOps.socketBatchClose(disuseLinger, closeConn, (socketCloseData->txData).num);
    if (*opResult != 0) {
        hccp_err("socket batch close failed ret[%d].", *opResult);
    }

    return 0;
}

STATIC int RaRsSocketBatchAbort(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpSocketConnectData *socketConnectData = (union OpSocketConnectData *)(inBuf +
        sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpSocketConnectData), sizeof(struct MsgHead), rcvBufLen,
        opResult);
    HCCP_CHECK_PARAM_NUM(socketConnectData->txData.num, MAX_SOCKET_NUM);

    *opResult = gRaRsOps.socketBatchAbort((socketConnectData->txData).conn,
        (socketConnectData->txData).num);
    if (*opResult != 0) {
        hccp_err("socket batch abort failed ret[%d]", *opResult);
    }

    return 0;
}

STATIC int RaRsSocketListenStart(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpSocketListenData *socketListenData = (union OpSocketListenData *)(inBuf + sizeof(struct MsgHead));
    union OpSocketListenData *socketListenDataReturn = NULL;
    unsigned int usePort = 0;
    unsigned int i;
    int ret;

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpSocketListenData), sizeof(struct MsgHead), rcvBufLen, opResult);

    // clear resv bit 31 use_port, for compatibility issue
    usePort = socketListenData->txData.num >> SOCKET_USE_PORT_BIT;
    socketListenData->txData.num &= ~(1U << SOCKET_USE_PORT_BIT);
    HCCP_CHECK_PARAM_LEN_RET_HOST(socketListenData->txData.num, 0, MAX_SOCKET_NUM, opResult);

    for (i = 0; i < (socketListenData->txData).num; i++) {
        // use_port flag not specify, use default port for compatibility issue
        if (usePort == 0) {
            (socketListenData->txData).conn[i].port = RS_SOCK_PORT_DEF;
        } else if ((socketListenData->txData).conn[i].port > MAX_PORT_NUM) {
            hccp_err("[listen_start]conn[%u].port=%u invalid", i, (socketListenData->txData).conn[i].port);
            return -EINVAL;
        }
    }
    *opResult = gRaRsOps.socketListenStart((socketListenData->txData).conn, (socketListenData->txData).num);
    if (*opResult == -EADDRINUSE) {
        hccp_run_warn("socket listen start unsuccessful ret[%d]", *opResult);
        return 0;
    } else if (*opResult != 0) {
        hccp_err("socket listen start failed ret[%d]", *opResult);
        return 0;
    }

    socketListenDataReturn = (union OpSocketListenData *)(outBuf + sizeof(struct MsgHead));
    ret = memcpy_s((socketListenDataReturn->rxData).conn, sizeof(struct SocketListenInfo) * MAX_SOCKET_NUM,
        (socketListenData->txData).conn, sizeof(struct SocketListenInfo) * (socketListenData->txData).num);
    CHK_PRT_RETURN(ret, hccp_err("memcpy_s socket_listen_info failed, ret[%d]", ret), -ESAFEFUNC);
    return 0;
}

STATIC int RaRsSocketListenStop(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpSocketListenData *socketListenData = (union OpSocketListenData *)(inBuf + sizeof(struct MsgHead));
    unsigned int usePort = 0;
    unsigned int i;

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpSocketListenData), sizeof(struct MsgHead), rcvBufLen, opResult);

    // clear resv bit 31 use_port, for compatibility issue
    usePort = socketListenData->txData.num >> SOCKET_USE_PORT_BIT;
    socketListenData->txData.num &= ~(1U << SOCKET_USE_PORT_BIT);
    HCCP_CHECK_PARAM_LEN_RET_HOST(socketListenData->txData.num, 0, MAX_SOCKET_NUM, opResult);

    for (i = 0; i < (socketListenData->txData).num; i++) {
        // use_port flag not specify, use default port for compatibility issue
        if (usePort == 0) {
            (socketListenData->txData).conn[i].port = RS_SOCK_PORT_DEF;
        } else if ((socketListenData->txData).conn[i].port > MAX_PORT_NUM) {
            hccp_err("[listen_stop]conn[%u].port=%u invalid", i, (socketListenData->txData).conn[i].port);
            return -EINVAL;
        }
    }

    *opResult = gRaRsOps.socketListenStop((socketListenData->txData).conn, (socketListenData->txData).num);
    if (*opResult != 0) {
        hccp_err("socket listen stop failed ret[%d].", *opResult);
    }

    return 0;
}

STATIC int RaRsGetSockets(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    int ret;
    union OpSocketInfoData *socketInfoDataReturn = NULL;
    union OpSocketInfoData *socketInfoData = (union OpSocketInfoData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpSocketInfoData), sizeof(struct MsgHead), rcvBufLen, opResult);
    HCCP_CHECK_PARAM_LEN_RET_HOST(socketInfoData->txData.num, 0, MAX_SOCKET_NUM, opResult);

    *opResult = gRaRsOps.getSockets(socketInfoData->txData.role, socketInfoData->txData.conn,
        socketInfoData->txData.num);
    if (*opResult < 0) {
        hccp_err("socket info get failed ret[%d].", *opResult);
        return 0;
    }

    socketInfoDataReturn = (union OpSocketInfoData *)(outBuf + sizeof(struct MsgHead));

    (socketInfoDataReturn->rxData).num = *opResult;
    ret = memcpy_s((socketInfoDataReturn->rxData).conn, sizeof(struct SocketFdData) * MAX_SOCKET_NUM,
        (socketInfoData->txData).conn, sizeof(struct SocketFdData) * (socketInfoData->txData).num);
    CHK_PRT_RETURN(ret, hccp_err("ra_rs_get_sockets memcpy_s failed, ret[%d]. ", ret), -ESAFEFUNC);

    return 0;
}

STATIC int RaRsSocketRecv(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    int recvLen;
    union OpSocketRecvData *recvData = (union OpSocketRecvData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpSocketRecvData), sizeof(struct MsgHead), rcvBufLen, opResult);
    HCCP_CHECK_PARAM_LEN(sizeof(union OpSocketRecvData) + recvData->txData.recvSize, sizeof(struct MsgHead),
        rcvBufLen);

    recvLen = gRaRsOps.socketRecv(recvData->txData.fd,
        outBuf + sizeof(struct MsgHead) + sizeof(union OpSocketRecvData), recvData->txData.recvSize);
    *opResult = recvLen;

    recvData = (union OpSocketRecvData *)(outBuf + sizeof(struct MsgHead));
    recvData->rxData.realRecvSize = recvLen;

    return 0;
}

STATIC int RaRsSocketSend(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    int sendLen;
    union OpSocketSendData *sendData = (union OpSocketSendData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpSocketSendData), sizeof(struct MsgHead), rcvBufLen, opResult);
    HCCP_CHECK_PARAM_LEN_RET_HOST(sendData->txData.sendSize, 0, SOCKET_SEND_MAXLEN, opResult);

    sendLen =
        gRaRsOps.socketSend(sendData->txData.fd, sendData->txData.dataSend, sendData->txData.sendSize);
    if (sendLen <= 0) {
        if (sendLen == -EAGAIN) {
            hccp_dbg("socket send need retry, ret[%d]", sendLen);
        }else {
            hccp_warn("send unsuccessful, sendLen[%d] expect greater than 0.", sendLen);
        }
    }

    *opResult = sendLen;
    sendData = (union OpSocketSendData *)(outBuf + sizeof(struct MsgHead));
    sendData->rxData.realSendSize = sendLen;

    return 0;
}

STATIC int RaRsRdevInit(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    unsigned int rdevIndex = 0;
    union OpRdevInitData *rdevInitData = (union OpRdevInitData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpRdevInitData), sizeof(struct MsgHead), rcvBufLen, opResult);

    *opResult = gRaRsOps.rdevInit(rdevInitData->txData.rdevInfo, NOTIFY, &rdevIndex);
    if (*opResult != 0) {
        hccp_err("rdev_init failed ret[%d].", *opResult);
        return 0;
    }

    rdevInitData = (union OpRdevInitData *)(outBuf + sizeof(struct MsgHead));
    rdevInitData->rxData.rdevIndex = rdevIndex;

    return 0;
}

STATIC int RaRsRdevInitWithBackup(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpRdevInitWithBackupData *rdevInitData = (union OpRdevInitWithBackupData *)(inBuf +
        sizeof(struct MsgHead));
    unsigned int rdevIndex = 0;

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpRdevInitWithBackupData), sizeof(struct MsgHead),
        rcvBufLen, opResult);

    *opResult = gRaRsOps.rdevInitWithBackup(rdevInitData->txData.rdevInfo,
        rdevInitData->txData.backupRdevInfo, NOTIFY, &rdevIndex);
    if (*opResult != 0) {
        hccp_err("rdev_init_with_backup failed ret[%d].", *opResult);
        return 0;
    }

    rdevInitData = (union OpRdevInitWithBackupData *)(outBuf + sizeof(struct MsgHead));
    rdevInitData->rxData.rdevIndex = rdevIndex;

    return 0;
}

STATIC int RaRsRdevGetPortStatus(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpRdevGetPortStatusData *statusData = NULL;
    enum PortStatus status = PORT_STATUS_DOWN;

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpRdevGetPortStatusData), sizeof(struct MsgHead), rcvBufLen,
        opResult);

    statusData = (union OpRdevGetPortStatusData *)(inBuf + sizeof(struct MsgHead));
    *opResult = gRaRsOps.rdevGetPortStatus(statusData->txData.phyId,
        statusData->txData.rdevIndex, &status);
    if (*opResult != 0) {
        hccp_err("rdev_get_port_status failed ret[%d].", *opResult);
        return 0;
    }

    statusData = (union OpRdevGetPortStatusData *)(outBuf + sizeof(struct MsgHead));
    statusData->rxData.status = status;

    return 0;
}

STATIC int RaRsRdevDeinit(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpRdevDeinitData *rdevDeinitData = (union OpRdevDeinitData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpRdevDeinitData), sizeof(struct MsgHead), rcvBufLen, opResult);

    *opResult = gRaRsOps.rdevDeinit(rdevDeinitData->txData.phyId, NOTIFY,
        rdevDeinitData->txData.rdevIndex);
    if (*opResult != 0) {
        hccp_err("rdev_deinit failed ret[%d].", *opResult);
    }

    return 0;
}

STATIC int RaRsSocketInit(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpSocketInitData *socketInitData = (union OpSocketInitData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpSocketInitData), sizeof(struct MsgHead), rcvBufLen, opResult);

    *opResult = gRaRsOps.socketInit(socketInitData->txData.vnicIp, socketInitData->txData.num);
    if (*opResult != 0) {
        hccp_err("socket init failed ret[%d].", *opResult);
    }

    return 0;
}

STATIC int RaRsSocketDeinit(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpSocketDeinitData *socketDeinitData = (union OpSocketDeinitData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpSocketDeinitData), sizeof(struct MsgHead), rcvBufLen, opResult);

    *opResult = gRaRsOps.socketDeinit(socketDeinitData->txData.rdevInfo);
    if (*opResult != 0) {
        hccp_err("socket deinit failed ret[%d].", *opResult);
    }

    return 0;
}

STATIC int RaRsGetTsqpDepth(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    unsigned int tempDepth = 0;
    unsigned int qpNum = 0;
    union OpGetTsqpDepthData *getTsqpDepthData = (union OpGetTsqpDepthData *)(inBuf +
        sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpGetTsqpDepthData), sizeof(struct MsgHead), rcvBufLen,
        opResult);

    *opResult = gRaRsOps.getTsqpDepth(getTsqpDepthData->txData.phyId,
        getTsqpDepthData->txData.rdevIndex, &tempDepth, &qpNum);
    if (*opResult != 0) {
        hccp_err("set_tsqp_depth failed ret[%d].", *opResult);
        return 0;
    }

    getTsqpDepthData = (union OpGetTsqpDepthData *)(outBuf + sizeof(struct MsgHead));
    getTsqpDepthData->rxData.tempDepth = tempDepth;
    getTsqpDepthData->rxData.qpNum = qpNum;

    return 0;
}

STATIC int RaRsSetTsqpDepth(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    unsigned int qpNum = 0;
    union OpSetTsqpDepthData *setTsqpDepthData = (union OpSetTsqpDepthData *)(inBuf +
        sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpSetTsqpDepthData), sizeof(struct MsgHead), rcvBufLen,
        opResult);

    *opResult = gRaRsOps.setTsqpDepth(setTsqpDepthData->txData.phyId,
        setTsqpDepthData->txData.rdevIndex, setTsqpDepthData->txData.tempDepth, &qpNum);
    if (*opResult != 0) {
        hccp_err("set_tsqp_depth failed ret[%d].", *opResult);
        return 0;
    }

    setTsqpDepthData = (union OpSetTsqpDepthData *)(outBuf + sizeof(struct MsgHead));
    setTsqpDepthData->rxData.qpNum = qpNum;

    return 0;
}

STATIC int RaRsQpCreate(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    struct RsQpNorm qpNorm;
    struct RsQpResp qpResp = { 0 };
    union OpQpCreateData *createData = (union OpQpCreateData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpQpCreateData), sizeof(struct MsgHead), rcvBufLen, opResult);

    int qpMode = createData->txData.qpMode;
    qpNorm.flag = createData->txData.flag;
    qpNorm.isExp = 1;
    qpNorm.isExt = 1;
    if (qpMode == RA_RS_OP_QP_MODE_EXT) {
        qpNorm.qpMode = RA_RS_OP_QP_MODE;
    } else {
        qpNorm.qpMode = qpMode;
    }
    qpNorm.memAlign = createData->txData.memAlign;

    *opResult = gRaRsOps.qpCreate(createData->txData.phyId, createData->txData.rdevIndex, qpNorm, &qpResp);
    if (*opResult != 0) {
        hccp_err("qp create failed ret[%d].", *opResult);
        return 0;
    }

    createData = (union OpQpCreateData *)(outBuf + sizeof(struct MsgHead));
    createData->rxData.qpn = qpResp.qpn;
    createData->rxData.psn = qpResp.psn;
    createData->rxData.gidIdx = qpResp.gidIdx;

    return 0;
}

STATIC int RaRsQpCreateWithAttrs(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpQpCreateWithAttrsData *createData = NULL;
    struct RsQpNormWithAttrs qpNorm = { 0 };
    struct RsQpRespWithAttrs qpResp = { 0 };

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpQpCreateWithAttrsData), sizeof(struct MsgHead), rcvBufLen,
        opResult);

    createData = (union OpQpCreateWithAttrsData *)(inBuf + sizeof(struct MsgHead));

    qpNorm.isExp = 1;
    qpNorm.isExt = 1;
    qpNorm.extAttrs = createData->txData.extAttrs;

    *opResult = gRaRsOps.qpCreateWithAttrs(createData->txData.phyId, createData->txData.rdevIndex,
        &qpNorm, &qpResp);
    if (*opResult != 0) {
        hccp_err("qp create failed ret[%d].", *opResult);
        return 0;
    }

    createData = (union OpQpCreateWithAttrsData *)(outBuf + sizeof(struct MsgHead));
    createData->rxData.qpn = qpResp.qpn;
    createData->rxData.psn = qpResp.psn;
    createData->rxData.gidIdx = qpResp.gidIdx;

    return 0;
}

STATIC int RaRsAiQpCreate(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpAiQpCreateData *createData = NULL;
    struct RsQpNormWithAttrs qpNorm = { 0 };
    struct RsQpRespWithAttrs qpResp = { 0 };

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpAiQpCreateData), sizeof(struct MsgHead), rcvBufLen,
        opResult);

    createData = (union OpAiQpCreateData *)(inBuf + sizeof(struct MsgHead));

    qpNorm.isExp = 1;
    qpNorm.isExt = 1;
    qpNorm.extAttrs = createData->txData.extAttrs;
    qpNorm.aiOpSupport = 1;

    *opResult = gRaRsOps.qpCreateWithAttrs(createData->txData.phyId, createData->txData.rdevIndex,
        &qpNorm, &qpResp);
    if (*opResult != 0) {
        hccp_err("qp create failed ret[%d].", *opResult);
        return 0;
    }

    createData = (union OpAiQpCreateData *)(outBuf + sizeof(struct MsgHead));
    createData->rxData.qpn = qpResp.qpn;
    createData->rxData.aiQpAddr = qpResp.aiQpAddr;
    createData->rxData.sqIndex = qpResp.sqIndex;
    createData->rxData.dbIndex = qpResp.dbIndex;
    createData->rxData.psn = qpResp.psn;

    return 0;
}

STATIC int RaRsAiQpCreateWithData(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpAiQpCreateWithAttrsData *createData = NULL;
    struct RsQpNormWithAttrs qpNorm = { 0 };
    struct RsQpRespWithAttrs qpResp = { 0 };

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpAiQpCreateWithAttrsData), sizeof(struct MsgHead), rcvBufLen,
        opResult);

    createData = (union OpAiQpCreateWithAttrsData *)(inBuf + sizeof(struct MsgHead));

    qpNorm.isExp = 1;
    qpNorm.isExt = 1;
    qpNorm.extAttrs = createData->txData.extAttrs;
    qpNorm.aiOpSupport = 1;

    *opResult = gRaRsOps.qpCreateWithAttrs(createData->txData.phyId, createData->txData.rdevIndex,
        &qpNorm, &qpResp);
    if (*opResult != 0) {
        hccp_err("qp create failed ret[%d].", *opResult);
        return 0;
    }

    createData = (union OpAiQpCreateWithAttrsData *)(outBuf + sizeof(struct MsgHead));
    createData->rxData.qpn = qpResp.qpn;
    createData->rxData.gidIdx = qpResp.gidIdx;
    createData->rxData.psn = qpResp.psn;
    createData->rxData.aiQpAddr = qpResp.aiQpAddr;
    createData->rxData.sqIndex = qpResp.sqIndex;
    createData->rxData.dbIndex = qpResp.dbIndex;
    createData->rxData.aiScqAddr = qpResp.aiScqAddr;
    createData->rxData.aiRcqAddr = qpResp.aiRcqAddr;
    (void)memcpy_s(&createData->rxData.dataPlaneInfo, sizeof(struct AiDataPlaneInfo), &qpResp.dataPlaneInfo,
        sizeof(struct AiDataPlaneInfo));

    return 0;
}

STATIC int RaRsTypicalQpCreate(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpTypicalQpCreateData *createData = (union OpTypicalQpCreateData *)(inBuf +
        sizeof(struct MsgHead));
    struct RsQpResp qpResp = {0};
    struct RsQpNorm qpNorm;
    int qpMode;

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpTypicalQpCreateData), sizeof(struct MsgHead), rcvBufLen,
        opResult);

    qpMode = createData->txData.qpMode;
    qpNorm.flag = createData->txData.flag;
    qpNorm.isExp = 1;
    qpNorm.isExt = 1;
    if (qpMode == RA_RS_OP_QP_MODE_EXT) {
        qpNorm.qpMode = RA_RS_OP_QP_MODE;
    } else {
        qpNorm.qpMode = qpMode;
    }
    qpNorm.memAlign = createData->txData.memAlign;

    *opResult = gRaRsOps.qpCreate(createData->txData.phyId, createData->txData.rdevIndex, qpNorm, &qpResp);
    if (*opResult != 0) {
        hccp_err("qp create failed ret[%d].", *opResult);
        return 0;
    }

    createData = (union OpTypicalQpCreateData *)(outBuf + sizeof(struct MsgHead));
    createData->rxData.qpn = qpResp.qpn;
    createData->rxData.gidIdx = qpResp.gidIdx;
    createData->rxData.psn = qpResp.psn;
    createData->rxData.gid = qpResp.gid;

    return 0;
}

STATIC int RaRsQpDestroy(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpQpDestroyData *qpDestroyData = (union OpQpDestroyData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpQpDestroyData), sizeof(struct MsgHead), rcvBufLen, opResult);

    *opResult = gRaRsOps.qpDestroy(qpDestroyData->txData.phyId, qpDestroyData->txData.rdevIndex,
        qpDestroyData->txData.qpn);
    if (*opResult != 0) {
        hccp_err("qp destroy failed ret[%d].", *opResult);
    }

    return 0;
}

STATIC int RaRsTypicalQpModify(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpTypicalQpModifyData *qpModifyData = (union OpTypicalQpModifyData *)(inBuf +
        sizeof(struct MsgHead));
    unsigned int udpSport = 0;

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpTypicalQpModifyData), sizeof(struct MsgHead), rcvBufLen,
        opResult);

    *opResult = gRaRsOps.typicalQpModify(qpModifyData->txData.phyId, qpModifyData->txData.rdevIndex,
        qpModifyData->txData.localQpInfo, qpModifyData->txData.remoteQpInfo,
        &udpSport);
    if (*opResult != 0) {
        hccp_err("qp info modify failed ret[%d].", *opResult);
        return 0;
    }

    qpModifyData = (union OpTypicalQpModifyData *)(outBuf + sizeof(struct MsgHead));
    qpModifyData->rxData.udpSport = udpSport;

    return 0;
}

STATIC int RaRsQpBatchModify(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpQpBatchModifyData *qpBatchModifyData = (union OpQpBatchModifyData *)(inBuf +
        sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpQpBatchModifyData), sizeof(struct MsgHead), rcvBufLen,
        opResult);
    HCCP_CHECK_PARAM_LEN_RET_HOST(qpBatchModifyData->txData.qpnNum, 0, RA_MAX_BATCH_QP_MODIFY_NUM, opResult);

    *opResult = gRaRsOps.qpBatchModify(qpBatchModifyData->txData.phyId,
        qpBatchModifyData->txData.rdevIndex, qpBatchModifyData->txData.status,
        qpBatchModifyData->txData.qpn, qpBatchModifyData->txData.qpnNum);
    if (*opResult != 0) {
        hccp_err("qp info modify failed ret[%d].", *opResult);
    }

    return 0;
}

STATIC int RaRsQpConnectAsync(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpQpConnectData *qpConnectData = (union OpQpConnectData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpQpConnectData), sizeof(struct MsgHead), rcvBufLen, opResult);

    *opResult = gRaRsOps.qpConnectAsync(qpConnectData->txData.phyId, qpConnectData->txData.rdevIndex,
        qpConnectData->txData.qpn, qpConnectData->txData.fd);
    if (*opResult != 0) {
        hccp_err("qp info async failed ret[%d].", *opResult);
    }

    return 0;
}

STATIC int RaRsGetQpStatus(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpQpStatusData *qpStatusData = (union OpQpStatusData *)(inBuf + sizeof(struct MsgHead));
    struct RsQpStatusInfo qpInfo = { 0 };

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpQpStatusData), sizeof(struct MsgHead), rcvBufLen, opResult);

    *opResult = gRaRsOps.getQpStatus(qpStatusData->txData.phyId, qpStatusData->txData.rdevIndex,
        qpStatusData->txData.qpn, &qpInfo);
    if (*opResult != 0) {
        hccp_err("query qp status async failed ret[%d].", *opResult);
        return 0;
    }

    qpStatusData = (union OpQpStatusData *)(outBuf + sizeof(struct MsgHead));
    qpStatusData->rxData.status = qpInfo.status;

    return 0;
}

STATIC int RaRsGetQpInfo(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpQpInfoData *qpInfoData = (union OpQpInfoData *)(inBuf + sizeof(struct MsgHead));
    struct RsQpStatusInfo qpInfo = { 0 };

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpQpInfoData), sizeof(struct MsgHead), rcvBufLen, opResult);

    *opResult = gRaRsOps.getQpStatus(qpInfoData->txData.phyId, qpInfoData->txData.rdevIndex,
        qpInfoData->txData.qpn, &qpInfo);
    if (*opResult != 0) {
        hccp_err("query qp status async failed ret[%d].", *opResult);
        return 0;
    }

    qpInfoData = (union OpQpInfoData *)(outBuf + sizeof(struct MsgHead));
    qpInfoData->rxData.status = qpInfo.status;
    qpInfoData->rxData.udpSport = qpInfo.udpSport;

    return 0;
}

STATIC int RaRsMrReg(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpMrRegData *regMrData = (union OpMrRegData *)(inBuf + sizeof(struct MsgHead));
    struct RdmaMrRegInfo mrRegInfo = { 0 };

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpMrRegData), sizeof(struct MsgHead), rcvBufLen, opResult);

    mrRegInfo.addr = regMrData->txData.mrRegAttr.addr;
    mrRegInfo.len = regMrData->txData.mrRegAttr.len;
    mrRegInfo.access = regMrData->txData.mrRegAttr.access;
    *opResult = gRaRsOps.mrReg(regMrData->txData.phyId, regMrData->txData.rdevIndex,
        regMrData->txData.qpn, &mrRegInfo);
    if (*opResult != 0) {
        hccp_err("reg_mr failed ret[%d].", *opResult);
        return 0;
    }

    regMrData = (union OpMrRegData *)(outBuf + sizeof(struct MsgHead));
    regMrData->rxData.lkey = mrRegInfo.lkey;
    regMrData->rxData.rkey = mrRegInfo.rkey;

    return 0;
}

STATIC int RaRsMrDereg(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpMrDeregData *mrDeregData = (union OpMrDeregData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpMrDeregData), sizeof(struct MsgHead), rcvBufLen, opResult);

    *opResult = gRaRsOps.mrDereg(mrDeregData->txData.phyId, mrDeregData->txData.rdevIndex,
        mrDeregData->txData.qpn, mrDeregData->txData.addr);
    if (*opResult != 0) {
        hccp_err("dereg_mr failed ret[%d].", *opResult);
    }

    return 0;
}

STATIC int RaRsTypicalMrRegV1(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpTypicalMrRegData *regMrData = (union OpTypicalMrRegData *)(inBuf + sizeof(struct MsgHead));
    struct RdmaMrRegInfo mrRegInfo = { 0 };
    struct ibv_mr *raRsMrHandle = NULL;

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpTypicalMrRegData), sizeof(struct MsgHead),
        rcvBufLen, opResult);

    mrRegInfo.addr = regMrData->txData.mrRegAttr.addr;
    mrRegInfo.len = regMrData->txData.mrRegAttr.len;
    mrRegInfo.access = regMrData->txData.mrRegAttr.access;
    *opResult = gRaRsOps.registerMr(regMrData->txData.phyId, regMrData->txData.rdevIndex,
        &mrRegInfo, (void **)&raRsMrHandle);
    if (*opResult != 0) {
        hccp_err("reg_mr failed ret[%d].", *opResult);
        return 0;
    }

    regMrData = (union OpTypicalMrRegData *)(outBuf + sizeof(struct MsgHead));
    regMrData->rxData.lkey = mrRegInfo.lkey;
    regMrData->rxData.rkey = mrRegInfo.rkey;

    return 0;
}

STATIC int RaRsTypicalMrReg(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpTypicalMrRegData *regMrData = (union OpTypicalMrRegData *)(inBuf + sizeof(struct MsgHead));
    struct RdmaMrRegInfo mrRegInfo = { 0 };
    struct ibv_mr *raRsMrHandle = NULL;

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpTypicalMrRegData), sizeof(struct MsgHead),
        rcvBufLen, opResult);

    mrRegInfo.addr = regMrData->txData.mrRegAttr.addr;
    mrRegInfo.len = regMrData->txData.mrRegAttr.len;
    mrRegInfo.access = regMrData->txData.mrRegAttr.access;
    *opResult = gRaRsOps.typicalRegisterMr(regMrData->txData.phyId, regMrData->txData.rdevIndex,
        &mrRegInfo, (void **)&raRsMrHandle);
    if (*opResult != 0) {
        hccp_err("reg_mr failed ret[%d].", *opResult);
        return 0;
    }

    regMrData = (union OpTypicalMrRegData *)(outBuf + sizeof(struct MsgHead));
    regMrData->rxData.lkey = mrRegInfo.lkey;
    regMrData->rxData.rkey = mrRegInfo.rkey;
    regMrData->rxData.addr = (uint64_t)(uintptr_t)raRsMrHandle;

    return 0;
}

STATIC int RaRsRemapMr(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpRemapMrData *opData = (union OpRemapMrData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpRemapMrData), sizeof(struct MsgHead), rcvBufLen, opResult);
    HCCP_CHECK_PARAM_LEN_RET_HOST(opData->txData.memNum, 0, REMAP_MR_MAX_NUM, opResult);

    *opResult = gRaRsOps.remapMr(opData->txData.phyId, opData->txData.rdevIndex, opData->txData.memList,
        opData->txData.memNum);
    if (*opResult) {
        hccp_err("remap_mr failed ret[%d]", *opResult);
    }
    return 0;
}

STATIC int RaRsTypicalMrDereg(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpTypicalMrDeregData *mrDeregData =
        (union OpTypicalMrDeregData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpTypicalMrDeregData), sizeof(struct MsgHead),
        rcvBufLen, opResult);

    *opResult = gRaRsOps.typicalDeregisterMr(mrDeregData->txData.phyId, mrDeregData->txData.rdevIndex,
        mrDeregData->txData.addr);
    if (*opResult != 0) {
        hccp_err("dereg_mr failed ret[%d].", *opResult);
    }

    return 0;
}

STATIC int RaRsSendWr(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    int ret;

    struct SendWrRsp wrRsp = { 0 };
    union OpSendWrData *sendWrData = (union OpSendWrData *)(inBuf + sizeof(struct MsgHead));
    struct SendWr sWr = { 0 };

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpSendWrData), sizeof(struct MsgHead), rcvBufLen, opResult);

    sWr.bufNum = sendWrData->txData.bufNum;
    sWr.bufList = (struct SgList *)&sendWrData->txData.memList[0];
    sWr.dstAddr = sendWrData->txData.dstAddr;
    sWr.op = sendWrData->txData.op;
    sWr.sendFlag = sendWrData->txData.sendFlags;

    ret = gRaRsOps.sendWr(sendWrData->txData.phyId, sendWrData->txData.rdevIndex, sendWrData->txData.qpn,
        &sWr, &wrRsp);
    *opResult = ret;
    if (ret) {
        if (ret == -ENOENT) {
            hccp_warn("not found remote mr_info, need try again");
        } else {
            hccp_err("send wr failed ret[%d].", ret);
        }
    }

    sendWrData = (union OpSendWrData *)(outBuf + sizeof(struct MsgHead));
    sendWrData->rxData.wrRsp = wrRsp;

    return 0;
}

STATIC int RaRsSendWrList(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    int ret;
    uint32_t i;
    unsigned int completeNum = 0;
    struct SendWrRsp *wrRsp = NULL;
    struct WrInfo *wrList = NULL;
    struct RsWrlistBaseInfo baseInfo = {0};
    union OpSendWrlistData *sendWrlist = (union OpSendWrlistData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpSendWrlistData), sizeof(struct MsgHead), rcvBufLen, opResult);
    HCCP_CHECK_PARAM_LEN_RET_HOST(sendWrlist->txData.sendNum, 0,  MAX_WR_NUM, opResult);

    wrRsp = calloc(sendWrlist->txData.sendNum, sizeof(struct SendWrRsp));
    CHK_PRT_RETURN(wrRsp == NULL, hccp_err("wr_rsp calloc failed."), -ENOMEM);

    wrList = calloc(sendWrlist->txData.sendNum, sizeof(struct WrInfo));
    if (wrList == NULL) {
        hccp_err("wr_list calloc failed.");
        ret = -ENOMEM;
        goto alloc_wr_list_fail;
    }

    baseInfo.phyId = sendWrlist->txData.phyId;
    baseInfo.rdevIndex = sendWrlist->txData.rdevIndex;
    baseInfo.qpn = sendWrlist->txData.qpn;
    baseInfo.keyFlag = 0;

    for (i = 0; i < sendWrlist->txData.sendNum; i++) {
        wrList[i].op = sendWrlist->txData.wrlist[i].op;
        wrList[i].sendFlags = sendWrlist->txData.wrlist[i].sendFlags;
        wrList[i].dstAddr = sendWrlist->txData.wrlist[i].dstAddr;
        wrList[i].memList.addr = sendWrlist->txData.wrlist[i].memList.addr;
        wrList[i].memList.len = sendWrlist->txData.wrlist[i].memList.len;
        wrList[i].memList.lkey = sendWrlist->txData.wrlist[i].memList.lkey;
    }
    ret = gRaRsOps.sendWrList(baseInfo, wrList, sendWrlist->txData.sendNum, wrRsp, &completeNum);
    *opResult = ret;
    if (ret) {
        if (ret == -ENOENT) {
            hccp_warn("not found remote mr_info, need try again");
        } else {
            hccp_err("send wr failed ret[%d].", ret);
        }
    }
    sendWrlist = (union OpSendWrlistData *)(outBuf + sizeof(struct MsgHead));
    sendWrlist->rxData.completeNum = completeNum;
    ret = memcpy_s(sendWrlist->rxData.wrRsp, sizeof(struct SendWrRsp) * MAX_WR_NUM_V1, wrRsp,
        completeNum * sizeof(struct SendWrRsp));
    if (ret) {
        hccp_err("ra_rs_send_wr_list memcpy_s failed, ret[%d]. ", ret);
        ret = -ESAFEFUNC;
        goto copy_wr_rsp_fail;
    }

copy_wr_rsp_fail:
    free(wrList);
    wrList = NULL;

alloc_wr_list_fail:
    free(wrRsp);
    wrRsp = NULL;
    return 0;
}

STATIC void GetWrListV2(struct WrInfo *wrList, union OpSendWrlistDataV2 *sendWrlist)
{
    uint32_t i;
    for (i = 0; i < sendWrlist->txData.sendNum; i++) {
        wrList[i].op = sendWrlist->txData.wrlist[i].op;
        wrList[i].sendFlags = sendWrlist->txData.wrlist[i].sendFlags;
        wrList[i].dstAddr = sendWrlist->txData.wrlist[i].dstAddr;
        wrList[i].memList.addr = sendWrlist->txData.wrlist[i].memList.addr;
        wrList[i].memList.len = sendWrlist->txData.wrlist[i].memList.len;
        wrList[i].memList.lkey = sendWrlist->txData.wrlist[i].memList.lkey;
    }
    return;
}

STATIC int RaRsSendWrListV2(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    int ret;
    unsigned int completeNum = 0;
    struct WrInfo *wrList = NULL;
    struct SendWrRsp *wrRsp = NULL;
    struct RsWrlistBaseInfo baseInfo = {0};
    union OpSendWrlistDataV2 *sendWrlist = (union OpSendWrlistDataV2 *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpSendWrlistDataV2), sizeof(struct MsgHead), rcvBufLen,
        opResult);
    HCCP_CHECK_PARAM_LEN_RET_HOST(sendWrlist->txData.sendNum, 0, MAX_WR_NUM, opResult);

    wrRsp = calloc(sendWrlist->txData.sendNum, sizeof(struct SendWrRsp));
    CHK_PRT_RETURN(wrRsp == NULL, hccp_err("wr_rsp calloc failed."), -ENOMEM);

    wrList = calloc(sendWrlist->txData.sendNum, sizeof(struct WrInfo));
    if (wrList == NULL) {
        hccp_err("wr_list calloc failed.");
        ret = -ENOMEM;
        goto alloc_wr_list_fail;
    }

    baseInfo.phyId = sendWrlist->txData.phyId;
    baseInfo.rdevIndex = sendWrlist->txData.rdevIndex;
    baseInfo.qpn = sendWrlist->txData.qpn;
    baseInfo.keyFlag = 0;

    GetWrListV2(wrList, sendWrlist);
    ret = gRaRsOps.sendWrList(baseInfo, wrList, sendWrlist->txData.sendNum, wrRsp, &completeNum);
    *opResult = ret;
    if (ret) {
        if (ret == -ENOENT) {
            hccp_warn("not found remote mr_info, need try again");
        } else {
            hccp_err("send wr failed ret[%d].", ret);
        }
    }
    sendWrlist = (union OpSendWrlistDataV2 *)(outBuf + sizeof(struct MsgHead));
    sendWrlist->rxData.completeNum = completeNum;
    ret = memcpy_s(sendWrlist->rxData.wrRsp, sizeof(struct SendWrRsp) * MAX_WR_NUM, wrRsp,
        completeNum * sizeof(struct SendWrRsp));
    if (ret) {
        hccp_err("ra_rs_send_wr_list memcpy_s failed, ret[%d]. ", ret);
        ret = -ESAFEFUNC;
        goto copy_wr_rsp_fail;
    }

copy_wr_rsp_fail:
    free(wrList);
    wrList = NULL;

alloc_wr_list_fail:
    free(wrRsp);
    wrRsp = NULL;
    return 0;
}

STATIC void GetWrList(struct WrInfo *wrList, union OpSendWrlistDataExt *sendWrlist)
{
    uint32_t i;
    for (i = 0; i < sendWrlist->txData.sendNum; i++) {
        wrList[i].op = sendWrlist->txData.wrlist[i].op;
        wrList[i].sendFlags = sendWrlist->txData.wrlist[i].sendFlags;
        wrList[i].immData = sendWrlist->txData.wrlist[i].ext.immData;
        wrList[i].dstAddr = sendWrlist->txData.wrlist[i].dstAddr;
        wrList[i].memList.addr = sendWrlist->txData.wrlist[i].memList.addr;
        wrList[i].memList.len = sendWrlist->txData.wrlist[i].memList.len;
        wrList[i].memList.lkey = sendWrlist->txData.wrlist[i].memList.lkey;
        wrList[i].aux.dataType = sendWrlist->txData.wrlist[i].aux.dataType;
        wrList[i].aux.reduceType = sendWrlist->txData.wrlist[i].aux.reduceType;
        wrList[i].aux.notifyOffset = sendWrlist->txData.wrlist[i].aux.notifyOffset;
    }
    return;
}

STATIC int RaRsSendWrListExt(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    int ret;
    unsigned int completeNum = 0;
    struct SendWrRsp *wrRsp = NULL;
    struct WrInfo *wrList = NULL;
    struct RsWrlistBaseInfo baseInfo = {0};
    union OpSendWrlistDataExt *sendWrlist = (union OpSendWrlistDataExt *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpSendWrlistDataExt), sizeof(struct MsgHead), rcvBufLen,
        opResult);
    HCCP_CHECK_PARAM_LEN_RET_HOST(sendWrlist->txData.sendNum, 0, MAX_WR_NUM, opResult);

    wrRsp = calloc(sendWrlist->txData.sendNum, sizeof(struct SendWrRsp));
    CHK_PRT_RETURN(wrRsp == NULL, hccp_err("wr_rsp calloc failed."), -ENOMEM);

    wrList = calloc(sendWrlist->txData.sendNum, sizeof(struct WrInfo));
    if (wrList == NULL) {
        hccp_err("wr_list calloc failed.");
        ret = -ENOMEM;
        goto alloc_wr_list_fail;
    }

    baseInfo.phyId = sendWrlist->txData.phyId;
    baseInfo.rdevIndex = sendWrlist->txData.rdevIndex;
    baseInfo.qpn = sendWrlist->txData.qpn;
    baseInfo.keyFlag = 0;
    GetWrList(wrList, sendWrlist);
    ret = gRaRsOps.sendWrList(baseInfo, wrList, sendWrlist->txData.sendNum, wrRsp, &completeNum);
    *opResult = ret;
    if (ret) {
        if (ret == -ENOENT) {
            hccp_warn("not found remote mr_info, need try again");
        } else {
            hccp_err("send wr failed ret[%d].", ret);
        }
    }
    sendWrlist = (union OpSendWrlistDataExt *)(outBuf + sizeof(struct MsgHead));
    sendWrlist->rxData.completeNum = completeNum;
    ret = memcpy_s(sendWrlist->rxData.wrRsp, sizeof(struct SendWrRsp) * MAX_WR_NUM_V1, wrRsp,
        completeNum * sizeof(struct SendWrRsp));
    if (ret) {
        hccp_err("ra_rs_send_wr_list_ext memcpy_s failed, ret[%d]. ", ret);
        ret = -ESAFEFUNC;
        goto copy_wr_rsp_fail;
    }

copy_wr_rsp_fail:
    free(wrList);
    wrList = NULL;

alloc_wr_list_fail:
    free(wrRsp);
    wrRsp = NULL;
    return 0;
}

STATIC void GetWrListExtV2(struct WrInfo *wrList, union OpSendWrlistDataExtV2 *sendWrlist)
{
    uint32_t i;
    for (i = 0; i < sendWrlist->txData.sendNum; i++) {
        wrList[i].op = sendWrlist->txData.wrlist[i].op;
        wrList[i].sendFlags = sendWrlist->txData.wrlist[i].sendFlags;
        wrList[i].immData = sendWrlist->txData.wrlist[i].ext.immData;
        wrList[i].dstAddr = sendWrlist->txData.wrlist[i].dstAddr;
        wrList[i].memList.addr = sendWrlist->txData.wrlist[i].memList.addr;
        wrList[i].memList.len = sendWrlist->txData.wrlist[i].memList.len;
        wrList[i].memList.lkey = sendWrlist->txData.wrlist[i].memList.lkey;
        wrList[i].aux.dataType = sendWrlist->txData.wrlist[i].aux.dataType;
        wrList[i].aux.reduceType = sendWrlist->txData.wrlist[i].aux.reduceType;
        wrList[i].aux.notifyOffset = sendWrlist->txData.wrlist[i].aux.notifyOffset;
    }
    return;
}

STATIC int RaRsSendWrListExtV2(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    int ret;
    unsigned int completeNum = 0;
    struct WrInfo *wrList = NULL;
    struct SendWrRsp *wrRsp = NULL;
    struct RsWrlistBaseInfo baseInfo = {0};
    union OpSendWrlistDataExtV2 *sendWrlist = (union OpSendWrlistDataExtV2 *)(inBuf +
        sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpSendWrlistDataExtV2), sizeof(struct MsgHead), rcvBufLen,
        opResult);
    HCCP_CHECK_PARAM_LEN_RET_HOST(sendWrlist->txData.sendNum, 0, MAX_WR_NUM, opResult);

    wrRsp = calloc(sendWrlist->txData.sendNum, sizeof(struct SendWrRsp));
    CHK_PRT_RETURN(wrRsp == NULL, hccp_err("wr_rsp calloc failed."), -ENOMEM);

    wrList = calloc(sendWrlist->txData.sendNum, sizeof(struct WrInfo));
    if (wrList == NULL) {
        hccp_err("wr_list calloc failed.");
        ret = -ENOMEM;
        goto alloc_wr_list_fail;
    }

    baseInfo.phyId = sendWrlist->txData.phyId;
    baseInfo.rdevIndex = sendWrlist->txData.rdevIndex;
    baseInfo.qpn = sendWrlist->txData.qpn;
    baseInfo.keyFlag = 0;
    GetWrListExtV2(wrList, sendWrlist);
    ret = gRaRsOps.sendWrList(baseInfo, wrList, sendWrlist->txData.sendNum, wrRsp, &completeNum);
    *opResult = ret;
    if (ret) {
        if (ret == -ENOENT) {
            hccp_warn("not found remote mr_info, need try again");
        } else {
            hccp_err("send wr failed ret[%d].", ret);
        }
    }
    sendWrlist = (union OpSendWrlistDataExtV2 *)(outBuf + sizeof(struct MsgHead));
    sendWrlist->rxData.completeNum = completeNum;
    ret = memcpy_s(sendWrlist->rxData.wrRsp, sizeof(struct SendWrRsp) * MAX_WR_NUM, wrRsp,
        completeNum * sizeof(struct SendWrRsp));
    if (ret) {
        hccp_err("ra_rs_send_wr_list_ext_v2 memcpy_s failed, ret[%d]. ", ret);
        ret = -ESAFEFUNC;
        goto copy_wr_rsp_fail;
    }

copy_wr_rsp_fail:
    free(wrList);
    wrList = NULL;

alloc_wr_list_fail:
    free(wrRsp);
    wrRsp = NULL;
    return 0;
}

STATIC int RaRsSendNormalWrlist(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpSendNormalWrlistData *sendWrlistOut = (union OpSendNormalWrlistData *)(outBuf +
        sizeof(struct MsgHead));
    union OpSendNormalWrlistData *sendWrlist = (union OpSendNormalWrlistData *)(inBuf +
        sizeof(struct MsgHead));
    struct RsWrlistBaseInfo baseInfo = {0};

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpSendNormalWrlistData), sizeof(struct MsgHead), rcvBufLen,
        opResult);
    HCCP_CHECK_PARAM_LEN_RET_HOST(sendWrlist->txData.sendNum, 0, MAX_WR_NUM, opResult);

    baseInfo.phyId = sendWrlist->txData.phyId;
    baseInfo.rdevIndex = sendWrlist->txData.rdevIndex;
    baseInfo.qpn = sendWrlist->txData.qpn;
    baseInfo.keyFlag = 1;

    *opResult = gRaRsOps.sendWrList(baseInfo, sendWrlist->txData.wrlist, sendWrlist->txData.sendNum,
        sendWrlistOut->rxData.wrRsp, &sendWrlistOut->rxData.completeNum);
    if (*opResult != 0) {
        hccp_err("send_wr_list failed ret[%d].", *opResult);
    }
    return 0;
}

STATIC int RaRsGetNotifyBa(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpGetNotifyBaData *getNotifyBaData = (union OpGetNotifyBaData *)(inBuf + sizeof(struct MsgHead));
    struct MrInfoT info = { 0 };

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpGetNotifyBaData), sizeof(struct MsgHead), rcvBufLen, opResult);

    *opResult = gRaRsOps.getNotifyMrInfo(getNotifyBaData->txData.phyId,
        getNotifyBaData->txData.rdevIndex, &info);
    if (*opResult != 0) {
        hccp_err("reg_notify_mr failed ret[%d].", *opResult);
        return 0;
    }

    getNotifyBaData = (union OpGetNotifyBaData *)(outBuf + sizeof(struct MsgHead));
    getNotifyBaData->rxData.va = (unsigned long long)info.addr;
    getNotifyBaData->rxData.size = info.size;
    getNotifyBaData->rxData.access = info.access;
    getNotifyBaData->rxData.lkey = info.lkey;

    return 0;
}

STATIC int RaRsNotifyCfgSet(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpNotifyCfgSetData *setNotifyBaData =
        (union OpNotifyCfgSetData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpNotifyCfgSetData), sizeof(struct MsgHead), rcvBufLen,
        opResult);

    *opResult = gRaRsOps.notifyCfgSet(setNotifyBaData->txData.phyId, setNotifyBaData->txData.va,
        setNotifyBaData->txData.size);
    if (*opResult != 0) {
        hccp_err("notify_cfg_set failed ret[%d].", *opResult);
    }

    return 0;
}

STATIC int RaRsNotifyCfgGet(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    unsigned long long va = 0;
    unsigned long long size = 0;
    union OpNotifyCfgGetData *getNotifyBaData =
        (union OpNotifyCfgGetData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpNotifyCfgGetData), sizeof(struct MsgHead), rcvBufLen,
        opResult);

    *opResult = gRaRsOps.notifyCfgGet(getNotifyBaData->txData.phyId, &va, &size);
    if (*opResult != 0) {
        hccp_err("notify_cfg_set failed ret[%d].", *opResult);
        return 0;
    }

    getNotifyBaData = (union OpNotifyCfgGetData *)(outBuf + sizeof(struct MsgHead));
    getNotifyBaData->rxData.va = va;
    getNotifyBaData->rxData.size = size;
    return 0;
}

STATIC int RaSetPid(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpSetPidData *setPidData = (union OpSetPidData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpSetPidData), sizeof(struct MsgHead), rcvBufLen, opResult);

    hccp_info("ra get pid is [%d]", setPidData->txData.pid);

    *opResult = gRaRsOps.setHostPid(setPidData->txData.phyId, setPidData->txData.pid,
        setPidData->txData.pidSign);

    hccp_info("ra_set_pid finish");
    return 0;
}

STATIC int RaRsCloseHdcSession(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    *opResult = 0;
    hccp_info("ra_rs_close_hdc_session finish");
    return 0;
}

STATIC int RaRsGetVnicIp(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    unsigned int vnicIp = 0;
    union OpGetVnicIpData *vnicIpDataRet = NULL;
    union OpGetVnicIpData *vnicIpData = (union OpGetVnicIpData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpGetVnicIpData), sizeof(struct MsgHead), rcvBufLen, opResult);

    *opResult = gRaRsOps.getVnicIp(vnicIpData->txData.phyId, &vnicIp);
    if (*opResult != 0) {
        hccp_err("rs get vnic ip failed, phyId %d, ret %d", vnicIpData->txData.phyId, *opResult);
        return 0;
    }

    vnicIpDataRet = (union OpGetVnicIpData *)(outBuf + sizeof(struct MsgHead));
    hccp_info("rs get vnic_ip, phyId %d, vnicIp 0x%x", vnicIpData->txData.phyId, vnicIp);
    vnicIpDataRet->rxData.vnicIp = vnicIp;
    return 0;
}

STATIC int RaRsGetVnicIpInfosV1(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpGetVnicIpInfosDataV1 *vnicIpData = (union OpGetVnicIpInfosDataV1 *)(inBuf +
        sizeof(struct MsgHead));
    union OpGetVnicIpInfosDataV1 *vnicIpOut = (union OpGetVnicIpInfosDataV1 *)(outBuf +
        sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpGetVnicIpInfosDataV1), sizeof(struct MsgHead), rcvBufLen,
        opResult);
    HCCP_CHECK_PARAM_LEN_RET_HOST(vnicIpData->txData.num, 0, MAX_IP_INFO_NUM_V1, opResult);

    *opResult = gRaRsOps.getVnicIpInfos(vnicIpData->txData.phyId, vnicIpData->txData.type,
        vnicIpData->txData.ids, vnicIpData->txData.num, vnicIpOut->rxData.infos);

    if (*opResult != 0) {
        hccp_err("rs get vnic ip infos failed, ret %d", *opResult);
    }

    return 0;
}

STATIC int RaRsGetVnicIpInfos(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpGetVnicIpInfosData *vnicIpData = (union OpGetVnicIpInfosData *)(inBuf +
        sizeof(struct MsgHead));
    union OpGetVnicIpInfosData *vnicIpOut = (union OpGetVnicIpInfosData *)(outBuf +
        sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpGetVnicIpInfosData), sizeof(struct MsgHead), rcvBufLen,
        opResult);
    HCCP_CHECK_PARAM_LEN_RET_HOST(vnicIpData->txData.num, 0, MAX_IP_INFO_NUM, opResult);

    *opResult = gRaRsOps.getVnicIpInfos(vnicIpData->txData.phyId, vnicIpData->txData.type,
        vnicIpData->txData.ids, vnicIpData->txData.num, vnicIpOut->rxData.infos);

    if (*opResult != 0) {
        hccp_err("rs get vnic ip infos failed, ret %d", *opResult);
    }

    return 0;
}

STATIC int RaRsGetInterfaceVersion(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    unsigned int version = 0;
    union OpGetVersionData *versionInfoRet = NULL;
    union OpGetVersionData *versionInfo = (union OpGetVersionData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpGetVersionData), sizeof(struct MsgHead), rcvBufLen, opResult);

    *opResult = gRaRsOps.getInterfaceVersion(versionInfo->txData.opcode, &version);
    if (*opResult != 0) {
        hccp_err("get_interface_version failed, opcode %d, ret %d", versionInfo->txData.opcode, *opResult);
        return 0;
    }

    versionInfoRet = (union OpGetVersionData *)(outBuf + sizeof(struct MsgHead));
    versionInfoRet->rxData.version = version;
    return 0;
}

STATIC int RaRsSocketWhiteListAdd(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpWlistData *wlistData = (union OpWlistData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpWlistData), sizeof(struct MsgHead), rcvBufLen, opResult);
    HCCP_CHECK_PARAM_NUM(wlistData->txData.num, MAX_WLIST_NUM);

    *opResult = gRaRsOps.whiteListAdd(wlistData->txData.rdevInfo, wlistData->txData.wlist,
        wlistData->txData.num);
    if (*opResult != 0) {
        hccp_err("white_list_add failed, ret[%d]", *opResult);
    }
    return 0;
}

STATIC int RaRsSocketWhiteListAddV2(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpWlistDataV2 *wlistData = (union OpWlistDataV2 *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpWlistDataV2), sizeof(struct MsgHead), rcvBufLen, opResult);
    HCCP_CHECK_PARAM_NUM(wlistData->txData.num, MAX_WLIST_NUM);

    *opResult = gRaRsOps.whiteListAdd(wlistData->txData.rdevInfo, wlistData->txData.wlist,
        wlistData->txData.num);
    if (*opResult != 0) {
        hccp_err("white_list_add failed, ret[%d]", *opResult);
    }
    return 0;
}

STATIC int RaRsSocketWhiteListDel(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpWlistData *wlistData = (union OpWlistData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpWlistData), sizeof(struct MsgHead), rcvBufLen, opResult);
    HCCP_CHECK_PARAM_NUM(wlistData->txData.num, MAX_WLIST_NUM);

    *opResult = gRaRsOps.whiteListDel(wlistData->txData.rdevInfo, wlistData->txData.wlist,
        wlistData->txData.num);
    if (*opResult != 0) {
        hccp_err("white_list_del failed, ret[%d]", *opResult);
    }
    return 0;
}

STATIC int RaRsSocketWhiteListDelV2(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpWlistDataV2 *wlistData = (union OpWlistDataV2 *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpWlistDataV2), sizeof(struct MsgHead), rcvBufLen, opResult);
    HCCP_CHECK_PARAM_NUM(wlistData->txData.num, MAX_WLIST_NUM);

    *opResult = gRaRsOps.whiteListDel(wlistData->txData.rdevInfo, wlistData->txData.wlist,
        wlistData->txData.num);
    if (*opResult != 0) {
        hccp_err("white_list_del failed, ret[%d]", *opResult);
    }
    return 0;
}

STATIC int RaRsSocketCreditAdd(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpAcceptCreditData *opData = (union OpAcceptCreditData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpAcceptCreditData), sizeof(struct MsgHead), rcvBufLen, opResult);
    HCCP_CHECK_PARAM_NUM(opData->txData.num, MAX_SOCKET_NUM);

    *opResult = gRaRsOps.acceptCreditAdd(opData->txData.conn, opData->txData.num,
        opData->txData.creditLimit);
    if (*opResult != 0) {
        hccp_err("accept_credit_add failed, ret[%d]", *opResult);
    }
    return 0;
}

STATIC int RaRsGetIfnum(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpIfnumData *ifnumData = (union OpIfnumData *)(inBuf + sizeof(struct MsgHead));
    union OpIfnumData *ifnumDataReturn = NULL;
    unsigned int num = 0;
    bool isAll = false;

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpIfnumData), sizeof(struct MsgHead), rcvBufLen, opResult);
    /* resv bit 31 for is_all for compatibility issue */
    if ((ifnumData->txData.num & RA_RS_GET_ALL_IP_BIT_MASK) != 0) {
        isAll = true;
    }
    *opResult = gRaRsOps.getIfnum(ifnumData->txData.phyId, isAll, &num);
    if (*opResult != 0) {
        hccp_err("ra_rs_get_ifnum result ret[%d].", *opResult);
        return 0;
    }

    ifnumDataReturn = (union OpIfnumData *)(outBuf + sizeof(struct MsgHead));
    (ifnumDataReturn->rxData).num = num;

    return 0;
}

STATIC int RaRsGetIfaddrs(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    int ret;
    union OpIfaddrData *ifaddrDataReturn = NULL;
    union OpIfaddrData *ifaddrData = (union OpIfaddrData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpIfaddrData), sizeof(struct MsgHead), rcvBufLen, opResult);
    CHK_PRT_RETURN(ifaddrData->txData.num > MAX_INTERFACE_NUM || ifaddrData->txData.num == 0,
        hccp_err("interface number is invalid, num[%u]", ifaddrData->txData.num), -EINVAL);

    *opResult = gRaRsOps.getIfaddrs(ifaddrData->txData.ifaddrInfos, &(ifaddrData->txData.num),
        ifaddrData->txData.phyId);
    if (*opResult != 0) {
        hccp_err("ra_rs_get_ifaddrs result ret[%d].", *opResult);
        return 0;
    }

    ifaddrDataReturn = (union OpIfaddrData *)(outBuf + sizeof(struct MsgHead));

    (ifaddrDataReturn->rxData).num = ifaddrData->txData.num;
    ret = memcpy_s((ifaddrDataReturn->rxData).ifaddrInfos, sizeof(struct IfaddrInfo) * MAX_INTERFACE_NUM,
        (ifaddrData->txData).ifaddrInfos, sizeof(struct IfaddrInfo) * (ifaddrData->txData).num);
    CHK_PRT_RETURN(ret, hccp_err("ra_rs_get_sockets memcpy_s failed, ret[%d]. ", ret), -ESAFEFUNC);

    return 0;
}

STATIC int RaRsGetIfaddrsV2(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpIfaddrDataV2 *ifaddrData = (union OpIfaddrDataV2 *)(inBuf + sizeof(struct MsgHead));
    union OpIfaddrDataV2 *ifaddrDataReturn = NULL;
    bool isAll = false;
    int ret;

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpIfaddrDataV2), sizeof(struct MsgHead), rcvBufLen, opResult);

    /* resv bit 31 for is_all for compatibility issue */
    if ((ifaddrData->txData.num & RA_RS_GET_ALL_IP_BIT_MASK) != 0) {
        isAll = true;
    }
    ifaddrData->txData.num = ifaddrData->txData.num & (~RA_RS_GET_ALL_IP_BIT_MASK);
    CHK_PRT_RETURN(ifaddrData->txData.num > MAX_INTERFACE_NUM || ifaddrData->txData.num == 0,
        hccp_err("interface number of op_ifaddr_data_v2 is invalid, num[%u]", ifaddrData->txData.num), -EINVAL);

    *opResult = gRaRsOps.getIfaddrsV2(ifaddrData->txData.interfaceInfos, &(ifaddrData->txData.num),
        ifaddrData->txData.phyId, isAll);
    if (*opResult != 0) {
        hccp_err("ra_rs_get_ifaddrs_v2 result ret[%d].", *opResult);
        return 0;
    }

    ifaddrDataReturn = (union OpIfaddrDataV2 *)(outBuf + sizeof(struct MsgHead));

    (ifaddrDataReturn->rxData).num = ifaddrData->txData.num;
    ret = memcpy_s((ifaddrDataReturn->rxData).interfaceInfos, sizeof(struct InterfaceInfo) * MAX_INTERFACE_NUM,
        (ifaddrData->txData).interfaceInfos, sizeof(struct InterfaceInfo) * (ifaddrData->txData).num);
    CHK_PRT_RETURN(ret, hccp_err("ra_rs_get_ifaddrs_v2 memcpy_s failed, ret[%d].", ret), -ESAFEFUNC);

    return 0;
}

STATIC int RaRsSetQpAttrQos(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpSetQpAttrQosData *attrQosData = (union OpSetQpAttrQosData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpSetQpAttrQosData), sizeof(struct MsgHead), rcvBufLen,
        opResult);

    *opResult = gRaRsOps.setQpAttrQos(attrQosData->txData.phyId, attrQosData->txData.rdevIndex,
        attrQosData->txData.qpn, &(attrQosData->txData.qosAttr));
    if (*opResult != 0) {
        hccp_err("set_qp_attr_qos failed ret[%d].", *opResult);
    }

    return 0;
}

STATIC int RaRsSetQpAttrTimeout(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpSetQpAttrTimeoutData *attrTimeData =
        (union OpSetQpAttrTimeoutData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpSetQpAttrTimeoutData), sizeof(struct MsgHead), rcvBufLen,
        opResult);

    *opResult = gRaRsOps.setQpAttrTimeout(attrTimeData->txData.phyId, attrTimeData->txData.rdevIndex,
        attrTimeData->txData.qpn, &(attrTimeData->txData.timeout));
    if (*opResult != 0) {
        hccp_err("set_qp_attr_timeout failed ret[%d].", *opResult);
    }

    return 0;
}

STATIC int RaRsSetQpAttrRetryCnt(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpSetQpAttrRetryCntData *attrRetryCntData =
        (union OpSetQpAttrRetryCntData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpSetQpAttrRetryCntData), sizeof(struct MsgHead), rcvBufLen,
        opResult);

    *opResult =
        gRaRsOps.setQpAttrRetryCnt(attrRetryCntData->txData.phyId, attrRetryCntData->txData.rdevIndex,
        attrRetryCntData->txData.qpn, &(attrRetryCntData->txData.retryCnt));
    if (*opResult != 0) {
        hccp_err("set_qp_attr_retry_cnt failed ret[%d].", *opResult);
    }

    return 0;
}

STATIC int RaRsGetCqeErrInfo(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    int ret;
    struct CqeErrInfo info = { 0 };
    union OpGetCqeErrInfoData *cqeErrInfoRet = NULL;

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpGetCqeErrInfoData), sizeof(struct MsgHead), rcvBufLen,
        opResult);

    *opResult = gRaRsOps.getCqeErrInfo(&info);
    if (*opResult != 0) {
        hccp_err("get_cqe_err_info failed, ret %d", *opResult);
        return 0;
    }

    cqeErrInfoRet = (union OpGetCqeErrInfoData *)(outBuf + sizeof(struct MsgHead));
    ret = memcpy_s(&cqeErrInfoRet->rxData.info, sizeof(struct CqeErrInfo), &info, sizeof(struct CqeErrInfo));
    CHK_PRT_RETURN(ret, hccp_err("ra_rs_get_cqe_err_info memcpy_s failed, ret[%d]. ", ret), -ESAFEFUNC);
    return 0;
}

STATIC int RaRsGetLiteSupport(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpLiteSupportData *liteSupportData = (union OpLiteSupportData *)(inBuf + sizeof(struct MsgHead));
    union OpLiteSupportData *liteSupportOut = (union OpLiteSupportData *)(outBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpLiteSupportData), sizeof(struct MsgHead), rcvBufLen, opResult);

    *opResult = gRaRsOps.getLiteSupport(liteSupportData->txData.phyId,
        liteSupportData->txData.rdevIndex,
        &liteSupportOut->rxData.supportLite);
    if (*opResult != 0) {
        hccp_err("get_lite_support failed ret[%d].", *opResult);
    }

    return 0;
}

STATIC int RaRsGetLiteRdevCap(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpLiteRdevCapData *liteRdevCapData = (union OpLiteRdevCapData *)(inBuf + sizeof(struct MsgHead));
    union OpLiteRdevCapData *liteRdevCapOut = (union OpLiteRdevCapData *)(outBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpLiteRdevCapData), sizeof(struct MsgHead), rcvBufLen, opResult);

    *opResult = gRaRsOps.getLiteRdevCap(liteRdevCapData->txData.phyId,
        liteRdevCapData->txData.rdevIndex,
        (void *)&liteRdevCapOut->rxData.resp);
    if (*opResult != 0) {
        hccp_err("get_lite_rdev_cap failed ret[%d].", *opResult);
    }

    return 0;
}

STATIC int RaRsGetLiteQpCqAttr(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpLiteQpCqAttrData *liteQpCqAttrData =
        (union OpLiteQpCqAttrData *)(inBuf + sizeof(struct MsgHead));
    union OpLiteQpCqAttrData *liteQpCqAttrOut =
        (union OpLiteQpCqAttrData *)(outBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(
        sizeof(union OpLiteQpCqAttrData), sizeof(struct MsgHead), rcvBufLen, opResult);

    *opResult = gRaRsOps.getLiteQpCqAttr(liteQpCqAttrData->txData.phyId,
        liteQpCqAttrData->txData.rdevIndex,
        liteQpCqAttrData->txData.qpn,
        (void *)&liteQpCqAttrOut->rxData.resp);
    if (*opResult != 0) {
        hccp_err("get_lite_qp_cq_attr failed ret[%d].", *opResult);
    }

    return 0;
}

STATIC int RaRsGetLiteConnectedInfo(
    char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpLiteConnectedInfoData *liteConnectedInfoData =
        (union OpLiteConnectedInfoData *)(inBuf + sizeof(struct MsgHead));
    union OpLiteConnectedInfoData *liteConnectedInfoOut =
        (union OpLiteConnectedInfoData *)(outBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(
        sizeof(union OpLiteConnectedInfoData), sizeof(struct MsgHead), rcvBufLen, opResult);

    *opResult = gRaRsOps.getLiteConnectedInfo(liteConnectedInfoData->txData.phyId,
        liteConnectedInfoData->txData.rdevIndex,
        liteConnectedInfoData->txData.qpn,
        (void *)&liteConnectedInfoOut->rxData.resp);
    if (*opResult != 0) {
        hccp_err("get_lite_connected_info failed ret[%d].", *opResult);
    }

    return 0;
}

STATIC int RaRsGetLiteMemAttr(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpLiteMemAttrData *liteMemAttrData =
        (union OpLiteMemAttrData *)(inBuf + sizeof(struct MsgHead));
    union OpLiteMemAttrData *liteMemAttrOut =
        (union OpLiteMemAttrData *)(outBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(
        sizeof(union OpLiteMemAttrData), sizeof(struct MsgHead), rcvBufLen, opResult);

    *opResult = gRaRsOps.getLiteMemAttr(liteMemAttrData->txData.phyId,
        liteMemAttrData->txData.rdevIndex,
        liteMemAttrData->txData.qpn,
        (void *)&liteMemAttrOut->rxData.resp);
    if (*opResult != 0) {
        hccp_err("get_lite_mem_attr failed ret[%d].", *opResult);
    }

    return 0;
}

STATIC int RaRsPingInit(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpPingInitData *pingDataOut = (union OpPingInitData *)(outBuf + sizeof(struct MsgHead));
    union OpPingInitData *pingData = (union OpPingInitData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpPingInitData), sizeof(struct MsgHead), rcvBufLen, opResult);

    *opResult = gRaRsOps.pingInit(&pingData->txData.attr, &pingDataOut->rxData.info,
        &pingDataOut->rxData.devIndex);
    if (*opResult != 0) {
        hccp_err("ping_init failed ret[%d].", *opResult);
    }
    // only negative return value will be parsed
    if (*opResult > 0) {
        *opResult = -*opResult;
    }

    return 0;
}

STATIC int RaRsPingTargetAdd(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpPingAddData *pingData = (union OpPingAddData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpPingAddData), sizeof(struct MsgHead), rcvBufLen, opResult);

    *opResult = gRaRsOps.pingTargetAdd(&pingData->txData.rdev, &pingData->txData.target);
    if (*opResult != 0) {
        hccp_err("ping_target_add failed ret[%d].", *opResult);
    }
    // only negative return value will be parsed
    if (*opResult > 0) {
        *opResult = -*opResult;
    }

    return 0;
}

STATIC int RaRsPingTaskStart(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpPingStartData *pingData = (union OpPingStartData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpPingStartData), sizeof(struct MsgHead), rcvBufLen, opResult);

    *opResult = gRaRsOps.pingTaskStart(&pingData->txData.rdev, &pingData->txData.attr);
    if (*opResult != 0) {
        hccp_err("ping_task_start failed ret[%d].", *opResult);
    }
    // only negative return value will be parsed
    if (*opResult > 0) {
        *opResult = -*opResult;
    }

    return 0;
}

STATIC int RaRsPingGetResults(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpPingResultsData *pingDataOut = (union OpPingResultsData *)(outBuf + sizeof(struct MsgHead));
    union OpPingResultsData *pingData = (union OpPingResultsData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpPingResultsData), sizeof(struct MsgHead), rcvBufLen, opResult);
    HCCP_CHECK_PARAM_LEN_RET_HOST(pingData->txData.num, 0, RA_MAX_PING_TARGET_NUM, opResult);

    *opResult = gRaRsOps.pingGetResults(&pingData->txData.rdev, pingData->txData.target,
        &pingData->txData.num, pingDataOut->rxData.target);
    // caller needs to retry, degrade log level
    if (*opResult == -EAGAIN) {
        hccp_warn("ping_get_results unsuccessful, ret[%d].", *opResult);
    } else if (*opResult != 0) {
        hccp_err("ping_get_results failed, ret[%d].", *opResult);
    }
    // only negative return value will be parsed
    if (*opResult > 0) {
        *opResult = -*opResult;
        return 0;
    }
    pingDataOut->rxData.num = pingData->txData.num;

    return 0;
}

STATIC int RaRsPingTaskStop(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpPingStopData *pingData = (union OpPingStopData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpPingStopData), sizeof(struct MsgHead), rcvBufLen, opResult);

    *opResult = gRaRsOps.pingTaskStop(&pingData->txData.rdev);
    if (*opResult != 0) {
        hccp_err("ping_task_stop failed ret[%d].", *opResult);
    }
    // only negative return value will be parsed
    if (*opResult > 0) {
        *opResult = -*opResult;
    }

    return 0;
}

STATIC int RaRsPingTargetDel(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpPingDelData *pingData = (union OpPingDelData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpPingDelData), sizeof(struct MsgHead), rcvBufLen, opResult);
    HCCP_CHECK_PARAM_LEN_RET_HOST(pingData->txData.num, 0, RA_MAX_PING_TARGET_NUM, opResult);

    *opResult = gRaRsOps.pingTargetDel(&pingData->txData.rdev, pingData->txData.target,
        &pingData->txData.num);
    if (*opResult != 0) {
        hccp_err("ping_target_del failed ret[%d].", *opResult);
    }
    // only negative return value will be parsed
    if (*opResult > 0) {
        *opResult = -*opResult;
    }

    return 0;
}

STATIC int RaRsPingDeinit(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpPingDeinitData *pingData = (union OpPingDeinitData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpPingDeinitData), sizeof(struct MsgHead), rcvBufLen, opResult);

    *opResult = gRaRsOps.pingDeinit(&pingData->txData.rdev);
    if (*opResult != 0) {
        hccp_err("ping_deinit failed ret[%d].", *opResult);
    }
    // only negative return value will be parsed
    if (*opResult > 0) {
        *opResult = -*opResult;
    }

    return 0;
}

STATIC int RaRsGetCqeErrInfoNum(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpGetCqeErrInfoNumData *cqeErrInfoNum =
        (union OpGetCqeErrInfoNumData *)(inBuf + sizeof(struct MsgHead));
    unsigned int num;

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpGetCqeErrInfoNumData), sizeof(struct MsgHead), rcvBufLen,
        opResult);

    *opResult = gRaRsOps.getCqeErrInfoNum(cqeErrInfoNum->txData.phyId,
        cqeErrInfoNum->txData.rdevIndex, &num);
    if (*opResult != 0) {
        hccp_err("get_cqe_err_info_num failed, ret %d", *opResult);
        return 0;
    }

    cqeErrInfoNum = (union OpGetCqeErrInfoNumData *)(outBuf + sizeof(struct MsgHead));
    cqeErrInfoNum->rxData.num = num;

    return 0;
}

STATIC int RaRsGetCqeErrInfoList(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpGetCqeErrInfoListData *cqeErrInfoList =
        (union OpGetCqeErrInfoListData *)(inBuf + sizeof(struct MsgHead));
    union OpGetCqeErrInfoListData *cqeErrInfoListRet =
        (union OpGetCqeErrInfoListData *)(outBuf + sizeof(struct MsgHead));
    unsigned int num;

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpGetCqeErrInfoListData), sizeof(struct MsgHead), rcvBufLen,
        opResult);
    HCCP_CHECK_PARAM_LEN_RET_HOST(cqeErrInfoList->txData.num, 0, CQE_ERR_INFO_MAX_NUM, opResult);

    num = cqeErrInfoList->txData.num;
    *opResult = gRaRsOps.getCqeErrInfoList(cqeErrInfoList->txData.phyId,
        cqeErrInfoList->txData.rdevIndex, cqeErrInfoListRet->rxData.infoList, &num);
    if (*opResult != 0) {
        hccp_err("get_cqe_err_info_list failed, ret %d", *opResult);
        return 0;
    }

    cqeErrInfoListRet->rxData.num = num;

    return 0;
}

STATIC int RaRsGetTlsEnable(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpGetTlsEnableData *opDataRet = (union OpGetTlsEnableData *)(outBuf + sizeof(struct MsgHead));
    union OpGetTlsEnableData *opData = (union OpGetTlsEnableData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpGetTlsEnableData), sizeof(struct MsgHead), rcvBufLen,
        opResult);

    *opResult = gRaRsOps.getTlsEnable(opData->txData.phyId, &opDataRet->rxData.tlsEnable);
    if (*opResult != 0) {
        hccp_err("get_tls_enable failed, ret %d", *opResult);
    }
    return 0;
}

STATIC int RaRsGetSecRandom(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpGetSecRandomData *opDataRet = (union OpGetSecRandomData *)(outBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpGetSecRandomData), sizeof(struct MsgHead), rcvBufLen,
        opResult);

    *opResult = gRaRsOps.getSecRandom((int *)&opDataRet->rxData.value);
    if (*opResult != 0) {
        hccp_err("get sec random failed, ret %d", *opResult);
    }
    return 0;
}

STATIC int RaRsGetHccnCfg(char *inBuf, char *outBuf, int *outLen, int *opResult, int rcvBufLen)
{
    union OpGetHccnCfgData *opDataRet = (union OpGetHccnCfgData *)(outBuf + sizeof(struct MsgHead));
    union OpGetHccnCfgData *opData = (union OpGetHccnCfgData *)(inBuf + sizeof(struct MsgHead));

    HCCP_CHECK_PARAM_LEN_RET_HOST(sizeof(union OpGetHccnCfgData), sizeof(struct MsgHead), rcvBufLen,
        opResult);

    opDataRet->rxData.valueLen = HCCN_CFG_MSG_DATA_LEN;
    *opResult = gRaRsOps.getHccnCfg(opData->txData.phyId, opData->txData.key, opDataRet->rxData.value,
        &opDataRet->rxData.valueLen);
    if (*opResult != 0) {
        hccp_err("get hccn cfg failed, ret %d", *opResult);
    }
    return 0;
}

#define US_PRE_SECOND 1000000
#define US_PRE_MSECOND 1000
#define MS_PRE_SECOND 1000

STATIC void RaTimeInterval(struct timeval *endTime, struct timeval *startTime, long *msec)
{
    /* if low position is sufficient, then borrow one from the high position */
    if (endTime->tv_usec < startTime->tv_usec) {
        endTime->tv_sec -= 1;
        endTime->tv_usec += US_PRE_SECOND;
    }

    *msec = (endTime->tv_sec - startTime->tv_sec) * MS_PRE_SECOND +
        (endTime->tv_usec - startTime->tv_usec) / US_PRE_MSECOND;
}

#define OP_TYPE_CFG 0
#define OP_TYPE_QUERY 1

STATIC void RaGetOpRight(struct RaHdcOpSec *opSec, unsigned int opcode, unsigned int asyncReqId, int *right)
{
    long timeInterval = 0;
    struct timeval tCur;
    int exeRight;
    int ret;

    ret = gettimeofday(&tCur, NULL);
    if (ret) {
        *right = OP_RIGHT_QUERY_ERR;
        hccp_err("ra gettimeofday failed ret[%d].", ret);
        return;
    }

    RaTimeInterval(&tCur, &opSec->tLast, &timeInterval);
    opSec->tLast = (timeInterval < 0) ? tCur : opSec->tLast;
    timeInterval = (timeInterval < 0) ? 0 : timeInterval;
    opSec->tLast = (timeInterval == 0) ? opSec->tLast : tCur;
    opSec->tokenNum += timeInterval * TOKEN_RATE;

    if (opSec->tokenNum == 0) {
        *right = HAVE_NOT_OP_RIGHT;
        hccp_err("ra handle have not op right. opcode[%u].", opcode);
        return;
    }

    exeRight = HAVE_OP_RIGHT;

    opSec->tokenNum = opSec->tokenNum - 1;
    opSec->tokenNum = (opSec->tokenNum > BUCKET_DEPTH) ? BUCKET_DEPTH : opSec->tokenNum;
    if (opSec->isAsyncOp) {
        hccp_dbg("opcode[%u], reqId[%u], exeRight[%d], tokenNum[%llu], cfgOpNum[%u]",
            opcode, asyncReqId, exeRight, opSec->tokenNum, opSec->cfgOpNum);
    } else {
        hccp_dbg("opcode[%u], exeRight[%d], tokenNum[%llu], cfgOpNum[%u]",
            opcode, exeRight, opSec->tokenNum, opSec->cfgOpNum);
    }

    *right = exeRight;
    return;
}

struct RaOpHandle gRaOpHandle[] = {
    {RA_RS_SOCKET_CONN, RaRsSocketBatchConnect, sizeof(union OpSocketConnectData)},
    {RA_RS_SOCKET_CLOSE, RaRsSocketBatchClose, sizeof(union OpSocketCloseData)},
    {RA_RS_SOCKET_ABORT, RaRsSocketBatchAbort, sizeof(union OpSocketConnectData)},
    {RA_RS_SOCKET_LISTEN_START, RaRsSocketListenStart, sizeof(union OpSocketListenData)},
    {RA_RS_SOCKET_LISTEN_STOP, RaRsSocketListenStop, sizeof(union OpSocketListenData)},
    {RA_RS_GET_SOCKET, RaRsGetSockets, sizeof(union OpSocketInfoData)},
    {RA_RS_SOCKET_SEND, RaRsSocketSend, sizeof(union OpSocketSendData)},
    {RA_RS_SOCKET_RECV, RaRsSocketRecv, sizeof(union OpSocketRecvData)},
    {RA_RS_QP_CREATE, RaRsQpCreate, sizeof(union OpQpCreateData)},
    {RA_RS_QP_CREATE_WITH_ATTRS, RaRsQpCreateWithAttrs, sizeof(union OpQpCreateWithAttrsData)},
    {RA_RS_AI_QP_CREATE, RaRsAiQpCreate, sizeof(union OpAiQpCreateData)},
    {RA_RS_AI_QP_CREATE_WITH_ATTRS, RaRsAiQpCreateWithData, sizeof(union OpAiQpCreateWithAttrsData)},
    {RA_RS_TYPICAL_QP_CREATE, RaRsTypicalQpCreate, sizeof(union OpTypicalQpCreateData)},
    {RA_RS_QP_DESTROY, RaRsQpDestroy, sizeof(union OpQpDestroyData)},
    {RA_RS_TYPICAL_QP_MODIFY, RaRsTypicalQpModify, sizeof(union OpTypicalQpModifyData)},
    {RA_RS_QP_BATCH_MODIFY, RaRsQpBatchModify, sizeof(union OpQpBatchModifyData)},
    {RA_RS_QP_CONNECT, RaRsQpConnectAsync, sizeof(union OpQpConnectData)},
    {RA_RS_QP_STATUS, RaRsGetQpStatus, sizeof(union OpQpStatusData)},
    {RA_RS_QP_INFO, RaRsGetQpInfo, sizeof(union OpQpInfoData)},
    {RA_RS_MR_REG, RaRsMrReg, sizeof(union OpMrRegData)},
    {RA_RS_MR_DEREG, RaRsMrDereg, sizeof(union OpMrDeregData)},
    {RA_RS_TYPICAL_MR_REG_V1, RaRsTypicalMrRegV1, sizeof(union OpTypicalMrRegData)},
    {RA_RS_TYPICAL_MR_REG, RaRsTypicalMrReg, sizeof(union OpTypicalMrRegData)},
    {RA_RS_REMAP_MR, RaRsRemapMr, sizeof(union OpRemapMrData)},
    {RA_RS_TYPICAL_MR_DEREG, RaRsTypicalMrDereg, sizeof(union OpTypicalMrDeregData)},
    {RA_RS_SEND_WR, RaRsSendWr, sizeof(union OpSendWrData)},
    {RA_RS_GET_NOTIFY_BA, RaRsGetNotifyBa, sizeof(union OpGetNotifyBaData)},
    {RA_RS_SOCKET_INIT, RaRsSocketInit, sizeof(union OpSocketInitData)},
    {RA_RS_SOCKET_DEINIT, RaRsSocketDeinit, sizeof(union OpSocketDeinitData)},
    {RA_RS_RDEV_INIT, RaRsRdevInit, sizeof(union OpRdevInitData)},
    {RA_RS_RDEV_INIT_WITH_BACKUP, RaRsRdevInitWithBackup, sizeof(union OpRdevInitWithBackupData)},
    {RA_RS_RDEV_GET_PORT_STATUS, RaRsRdevGetPortStatus, sizeof(union OpRdevGetPortStatusData)},
    {RA_RS_RDEV_DEINIT, RaRsRdevDeinit, sizeof(union OpRdevDeinitData)},
    {RA_RS_WLIST_ADD, RaRsSocketWhiteListAdd, sizeof(union OpWlistData)},
    {RA_RS_WLIST_ADD_V2, RaRsSocketWhiteListAddV2, sizeof(union OpWlistDataV2)},
    {RA_RS_WLIST_DEL, RaRsSocketWhiteListDel, sizeof(union OpWlistData)},
    {RA_RS_WLIST_DEL_V2, RaRsSocketWhiteListDelV2, sizeof(union OpWlistDataV2)},
    {RA_RS_ACCEPT_CREDIT_ADD, RaRsSocketCreditAdd, sizeof(union OpAcceptCreditData)},
    {RA_RS_GET_IFNUM, RaRsGetIfnum, sizeof(union OpIfnumData)},
    {RA_RS_GET_IFADDRS, RaRsGetIfaddrs, sizeof(union OpIfaddrData)},
    {RA_RS_GET_IFADDRS_V2, RaRsGetIfaddrsV2, sizeof(union OpIfaddrDataV2)},
    {RA_RS_GET_INTERFACE_VERSION, RaRsGetInterfaceVersion, sizeof(union OpGetVersionData)},
    {RA_RS_SEND_WRLIST, RaRsSendWrList, sizeof(union OpSendWrlistData)},
    {RA_RS_SEND_WRLIST_V2, RaRsSendWrListV2, sizeof(union OpSendWrlistDataV2)},
    {RA_RS_SEND_WRLIST_EXT, RaRsSendWrListExt, sizeof(union OpSendWrlistDataExt)},
    {RA_RS_SEND_WRLIST_EXT_V2, RaRsSendWrListExtV2, sizeof(union OpSendWrlistDataExtV2)},
    {RA_RS_SEND_NORMAL_WRLIST, RaRsSendNormalWrlist, sizeof(union OpSendNormalWrlistData)},
    {RA_RS_SET_TSQP_DEPTH, RaRsSetTsqpDepth, sizeof(union OpSetTsqpDepthData)},
    {RA_RS_GET_TSQP_DEPTH, RaRsGetTsqpDepth, sizeof(union OpGetTsqpDepthData)},
    {RA_RS_HDC_SESSION_CLOSE, RaRsCloseHdcSession, sizeof(union OpSocketRecvData)},
    {RA_RS_GET_VNIC_IP, RaRsGetVnicIp, sizeof(union OpGetVnicIpData)},
    {RA_RS_GET_VNIC_IP_INFOS_V1, RaRsGetVnicIpInfosV1, sizeof(union OpGetVnicIpInfosDataV1)},
    {RA_RS_GET_VNIC_IP_INFOS, RaRsGetVnicIpInfos, sizeof(union OpGetVnicIpInfosData)},
    {RA_RS_NOTIFY_CFG_SET, RaRsNotifyCfgSet, sizeof(union OpNotifyCfgSetData)},
    {RA_RS_NOTIFY_CFG_GET, RaRsNotifyCfgGet, sizeof(union OpNotifyCfgGetData)},
    {RA_RS_SET_PID, RaSetPid, sizeof(union OpSetPidData)},
    {RA_RS_SET_QP_ATTR_QOS, RaRsSetQpAttrQos, sizeof(union OpSetQpAttrQosData)},
    {RA_RS_SET_QP_ATTR_TIMEOUT, RaRsSetQpAttrTimeout, sizeof(union OpSetQpAttrTimeoutData)},
    {RA_RS_SET_QP_ATTR_RETRY_CNT, RaRsSetQpAttrRetryCnt, sizeof(union OpSetQpAttrRetryCntData)},
    {RA_RS_GET_CQE_ERR_INFO, RaRsGetCqeErrInfo, sizeof(union OpGetCqeErrInfoData)},
    {RA_RS_GET_CQE_ERR_INFO_NUM, RaRsGetCqeErrInfoNum, sizeof(union OpGetCqeErrInfoNumData)},
    {RA_RS_GET_CQE_ERR_INFO_LIST, RaRsGetCqeErrInfoList, sizeof(union OpGetCqeErrInfoListData)},
    {RA_RS_GET_LITE_SUPPORT, RaRsGetLiteSupport, sizeof(union OpLiteSupportData)},
    {RA_RS_GET_LITE_RDEV_CAP, RaRsGetLiteRdevCap, sizeof(union OpLiteRdevCapData)},
    {RA_RS_GET_LITE_QP_CQ_ATTR, RaRsGetLiteQpCqAttr, sizeof(union OpLiteQpCqAttrData)},
    {RA_RS_GET_LITE_CONNECTED_INFO, RaRsGetLiteConnectedInfo, sizeof(union OpLiteConnectedInfoData)},
    {RA_RS_GET_LITE_MEM_ATTR, RaRsGetLiteMemAttr, sizeof(union OpLiteMemAttrData)},
    {RA_RS_PING_INIT, RaRsPingInit, sizeof(union OpPingInitData)},
    {RA_RS_PING_ADD, RaRsPingTargetAdd, sizeof(union OpPingAddData)},
    {RA_RS_PING_START, RaRsPingTaskStart, sizeof(union OpPingStartData)},
    {RA_RS_PING_GET_RESULTS, RaRsPingGetResults, sizeof(union OpPingResultsData)},
    {RA_RS_PING_STOP, RaRsPingTaskStop, sizeof(union OpPingStopData)},
    {RA_RS_PING_DEL, RaRsPingTargetDel, sizeof(union OpPingDelData)},
    {RA_RS_PING_DEINIT, RaRsPingDeinit, sizeof(union OpPingDeinitData)},
#ifdef CONFIG_TLV
    {RA_RS_TLV_INIT, RaRsTlvInit, sizeof(union OpTlvInitData)},
    {RA_RS_TLV_DEINIT, RaRsTlvDeinit, sizeof(union OpTlvDeinitData)},
    {RA_RS_TLV_REQUEST, RaRsTlvRequest, sizeof(union OpTlvRequestData)},
#endif
    {RA_RS_GET_TLS_ENABLE, RaRsGetTlsEnable, sizeof(union OpGetTlsEnableData)},
    {RA_RS_GET_SEC_RANDOM, RaRsGetSecRandom, sizeof(union OpGetSecRandomData)},
    {RA_RS_GET_HCCN_CFG, RaRsGetHccnCfg, sizeof(union OpGetHccnCfgData)},
    {RA_RS_ASYNC_HDC_SESSION_CONNECT, RaRsAsyncHdcSessionConnect, sizeof(union OpAsyncHdcConnectData)},
    {RA_RS_ASYNC_HDC_SESSION_CLOSE, RaRsAsyncHdcSessionClose, sizeof(union OpAsyncHdcCloseData)},
};

STATIC int RaCheckParam(char *recvBuf, int rcvBufLen, char **sendBuf, int *sndBufLen, int *paramCheckResult)
{
    int i;
    int ret = 0;
    struct MsgHead *recvMsgHead = (struct MsgHead *)recvBuf;
    int num = sizeof(gRaOpHandle) / sizeof(gRaOpHandle[0]);
    unsigned int dataSize = 0;

    *paramCheckResult = 1;
    if (rcvBufLen < (int)sizeof(struct MsgHead)) { // check rcv_buf_len
        hccp_err("rcv_buf_len[%d] form ra is invalid", rcvBufLen);
        ret = OpMsgErr(sendBuf, recvMsgHead, sndBufLen, RECV_BUF_LEN_INVALID);
        return ret;
    }

    if (((recvMsgHead->msgDataLen + sizeof(struct MsgHead)) != (unsigned int)rcvBufLen) ||
        (recvMsgHead->opcode >= RA_RS_OP_MAX_NUM ||
        ((recvMsgHead->opcode < RA_RS_HDC_SESSION_CLOSE) && (recvMsgHead->opcode >= RA_RS_EXTER_OP_MAX_NUM)))) {
        hccp_err("rcv data incomplete, because rcvBufLen[%d] != msg_head_len[%u] + msgDataLen[%u] \
            or opcode[%u] is wrong, RA_RS_OP_MAX_NUM:[%d], RA_RS_EXTER_OP_MAX_NUM:[%d]",
            rcvBufLen, sizeof(struct MsgHead), recvMsgHead->msgDataLen, recvMsgHead->opcode,
            RA_RS_OP_MAX_NUM, RA_RS_EXTER_OP_MAX_NUM);
        ret = OpMsgErr(sendBuf, recvMsgHead, sndBufLen, HAVE_OP_RIGHT);
        return ret;
    }
    for (i = 0; i < num; i++) {
        if (gRaOpHandle[i].opcode == recvMsgHead->opcode) {
            dataSize = gRaOpHandle[i].dataSize;
            break;
        }
    }
    if (recvMsgHead->opcode != RA_RS_SOCKET_RECV && recvMsgHead->msgDataLen != dataSize) {
        hccp_err("rcv data incomplete. because msg_data_len[%d] != op_data_len[%u]",
            recvMsgHead->msgDataLen, dataSize);
        ret = OpMsgErr(sendBuf, recvMsgHead, sndBufLen, RECV_BUF_LEN_INVALID);
        return ret;
    }
    *paramCheckResult = 0;
    return ret;
}

int RaHandle(struct RaHdcOpSec *opSec, char *recvBuf, int rcvBufLen, char **sendBuf, int *sndBufLen,
    unsigned int *closeSession)
{
    int i;
    int ret;
    int opRight = 0;
    int paramCheckRet = 0;
    int opRet = 0;
    struct MsgHead *recvMsgHead = (struct MsgHead *)recvBuf;
    int num = sizeof(gRaOpHandle) / sizeof(gRaOpHandle[0]);

    ret = RaCheckParam(recvBuf, rcvBufLen, sendBuf, sndBufLen, &paramCheckRet);
    CHK_PRT_RETURN(paramCheckRet != 0 || ret != 0, hccp_err("ra param check failed. param check ret:[%d]"
        "function call ret:[%d]", paramCheckRet, ret), ret);

    RaGetOpRight(opSec, recvMsgHead->opcode, recvMsgHead->asyncReqId, &opRight);
    CHK_PRT_RETURN(opRight != HAVE_OP_RIGHT, ret = OpMsgErr(sendBuf, recvMsgHead, sndBufLen, opRight), ret);

    *sendBuf = (char *)calloc(sizeof(char), recvMsgHead->msgDataLen + sizeof(struct MsgHead));
    CHK_PRT_RETURN(*sendBuf == NULL, hccp_err("calloc failed."), -ENOMEM);

    for (i = 0; i < num; i++) {
        if (gRaOpHandle[i].opcode == recvMsgHead->opcode) {
            ret = gRaOpHandle[i].opHandle(recvBuf, *sendBuf, sndBufLen, &opRet, rcvBufLen);
            if (ret) {
                hccp_err("ra handle failed. ret:[%d]", ret);
                goto out;
            }
            MsgHeadBuildUpHw(*sendBuf, recvMsgHead, opRet, recvMsgHead->msgDataLen);
            *closeSession = recvMsgHead->opcode == RA_RS_HDC_SESSION_CLOSE ? 1 : 0;
            *sndBufLen = recvMsgHead->msgDataLen + sizeof(struct MsgHead);
            return ret;
        }
    }

    hccp_warn("not support opcode:%d", recvMsgHead->opcode);
    ret = -EPROTONOSUPPORT;
out:
    free(*sendBuf);
    *sendBuf = NULL;
    return ret;
}

STATIC int RaSendPkt(struct drvHdcMsg *msgRcv, HDC_SESSION session, void *sendBuf, int sndBufLen)
{
    int ret;
    struct drvHdcMsg *msgSnd = NULL;

    ret = RA_HDC_OPS.reuseMsg(msgRcv);
    CHK_PRT_RETURN(ret, hccp_err("reuse msg failed ret %d", ret), ret);

    msgSnd = msgRcv;

    ret = RA_HDC_OPS.addMsgBuffer(msgSnd, sendBuf, sndBufLen);
    CHK_PRT_RETURN(ret, hccp_err("add msg buffer failed ret %d", ret), ret);

    ret = RA_HDC_OPS.send(session, msgSnd, 0, RA_HDC_RECV_SEND_TIMEOUT);
    CHK_PRT_RETURN(ret, hccp_err("send msg failed ret %d", ret), ret);

    return 0;
}

STATIC int RecvHandleSendPkt(HDC_SESSION session, unsigned int *closeSession, unsigned int chipId)
{
    int ret;
    void *recvBuf = NULL;
    void *sendBuf = NULL;
    struct drvHdcMsg *msgRcv = NULL;
    int recvBufCnt, sndBufLen, rcvBufLen;
    RsSetCtx(chipId);
    ret = RA_HDC_OPS.allocMsg(session, &msgRcv, 1);
    CHK_PRT_RETURN(ret, hccp_err("alloc hdc msg failed ret %d", ret), ret);

    ret = RA_HDC_OPS.recv(session, msgRcv, MAX_HDC_DATA, 0, &recvBufCnt, RA_HDC_RECV_SEND_TIMEOUT);
    if (ret) {
        hccp_warn("recv hdc msg unsuccessful, ret %d", ret);
        goto out;
    }

    RA_HDC_OPS.getMsgBuffer(msgRcv, 0, (char **)&recvBuf, &rcvBufLen);
    if (recvBuf == NULL) {
        hccp_warn("rcv_buf_len is NULL, Session disconnect.");
        goto out;
    }

    if (!rcvBufLen) {
        *closeSession = 1;
        hccp_warn("rcv_buf_len is 0, Session disconnect.");
        RA_HDC_OPS.freeMsg(msgRcv);
        return 0;
    }

    ret = RaHandle(&gHdcServer[chipId].opSec, recvBuf, rcvBufLen, (char **)&sendBuf, &sndBufLen,
        closeSession);
    if (ret) {
        hccp_err("ra_handle failed.");
        goto out;
    }

    ret = RaSendPkt(msgRcv, session, sendBuf, sndBufLen);
    if (ret) {
        hccp_err("ra send pkt failed ret %d", ret);
        goto err;
    }

err:
    free(sendBuf);
    sendBuf = NULL;
out:
    RA_HDC_OPS.freeMsg(msgRcv);
    return ret;
}

STATIC void RaHdcRecvHandleSendPkt(const unsigned int chipId)
{
    unsigned int closeSession = 0;
    int ret;

    ret = RecvHandleSendPkt(gHdcServer[chipId].hdcSession, &closeSession, chipId);
    if (closeSession || ret) {
        hccp_warn("recv_handle_send_pkt close_session[%u] ret[%d]", closeSession, ret);
        RA_PTHREAD_MUTEX_LOCK(&gHdcInitPara.mutex);
        gHdcInitPara.connectStatus = HDC_UNCONNECTED;
        RA_PTHREAD_MUTEX_UNLOCK(&gHdcInitPara.mutex);
    }

    return;
}

STATIC void RaHwHdcCloseSession(HDC_SESSION *session)
{
    int ret;

    RA_PTHREAD_MUTEX_LOCK(&gHdcInitPara.mutex);
    if (session == NULL || *session == NULL) {
        goto out;
    }

    ret = RA_HDC_OPS.sessionClose(*session);
    if (ret != 0) {
        hccp_warn("RA_HDC_OPS.sessionClose unsuccessful, ret:%d", ret);
    }
    *session = NULL;

out:
    RA_PTHREAD_MUTEX_UNLOCK(&gHdcInitPara.mutex);
    return;
}

STATIC void *RaPthread(void *arg)
{
    unsigned int chipId = gHdcInitPara.chipId;
    int ret;

    ret = pthread_detach(pthread_self());
    CHK_PRT_RETURN(ret, hccp_err("pthread detach failed ret %d", ret), NULL);

    (void)prctl(PR_SET_NAME, (unsigned long)"hccp_ra");

    RA_PTHREAD_MUTEX_LOCK(&gHdcInitPara.mutex);
    gHdcInitPara.threadStatus = THREAD_RUNNING;
    RA_PTHREAD_MUTEX_UNLOCK(&gHdcInitPara.mutex);

    RsGetCurTime(&gRaThreadInfo.lastCheckTime);
    ret = strncpy_s((char *)gRaThreadInfo.pthreadName, sizeof(gRaThreadInfo.pthreadName),
        "ra_thread", strlen("ra_thread"));
    CHK_PRT_RETURN(ret, hccp_err("strncpy_s pthread name failed, ret[%d]", ret), NULL);

    hccp_run_info("pthread[%s] is alive!", gRaThreadInfo.pthreadName);
    while (1) {
        if (gHdcInitPara.threadStatus == THREAD_DESTROYING) {
            break;
        }

        if (gHdcInitPara.connectStatus != HDC_CONNECTED) {
            usleep(THREAD_SLEEP_TIME);
            continue;
        }
        RsHeartbeatAlivePrint(&gRaThreadInfo);
        RaHdcRecvHandleSendPkt(chipId);
    }

    hccp_info("thread [%d] is out", getpid());
    RaHwHdcCloseSession(&gHdcServer[chipId].hdcSession);
    RA_PTHREAD_MUTEX_LOCK(&gHdcInitPara.mutex);
    gHdcInitPara.threadStatus = THREAD_HALT;
    RA_PTHREAD_MUTEX_UNLOCK(&gHdcInitPara.mutex);
    return NULL;
}

STATIC int RaHdcServerInit(unsigned int chipId, int hdcType)
{
    int ret;

    CHK_PRT_RETURN(chipId > HCCP_MAX_CHIP_ID || gHdcServer[chipId].hdcSession != NULL, hccp_err("invalid "
        "chip id %u, or hdcSession is not NULL", chipId), -EINVAL);
    CHK_PRT_RETURN(hdcType != HDC_SERVICE_TYPE_RDMA && hdcType != HDC_SERVICE_TYPE_RDMA_V2, hccp_err("invalid "
        "hdc_type %d", hdcType), -EINVAL);

    RaHdcInitOpSec(&gHdcServer[chipId].opSec, BUCKET_DEPTH, false);

    ret = RA_HDC_OPS.serverCreate(chipId, hdcType, &gHdcServer[chipId].hdcServer);
    CHK_PRT_RETURN(ret, hccp_err("Create Server failed, ret(%d) ", ret), -EINVAL);

    return 0;
}

void RaHdcInitOpSec(struct RaHdcOpSec *opSec, unsigned long long tokenNum, bool isAsyncOp)
{
    opSec->tokenNum = tokenNum;
    opSec->cfgOpNum = 0;
    opSec->tLast.tv_sec = 0;
    opSec->tLast.tv_usec = 0;
    opSec->isAsyncOp = isAsyncOp;
}

int RaHdcSessionAccept(unsigned int chipId, HDC_SESSION *session, int initHostTgid)
{
    int hostTgid;
    int ret = 0;

    ret = RA_HDC_OPS.sessionAccept(gHdcServer[chipId].hdcServer, session);
    if (ret != 0) {
        hccp_warn("Session accept failed, chipId(%u), ret(%d) ", chipId, ret);
        return ret;
    }

    RA_HDC_OPS.setSessionReference(*session);
    ret = DlHalHdcGetSessionAttr(*session, HDC_SESSION_ATTR_PEER_CREATE_PID, &hostTgid);
    if (ret) {
        hccp_err("Session get host_pid failed, chipId(%u), ret(%d)", chipId, ret);
        goto out;
    }

    if (hostTgid != initHostTgid) {
        hccp_warn("host_tgid[%d] from ra not equal to the tgid[%d] from hccp_init, invalid", hostTgid, initHostTgid);
        goto out;
    }

    return 0;

out:
    RaHdcCloseSession(session);
    return ret;
}

int RaHdcAsyncRecvPkt(struct RaHdcAsyncInfo *asyncInfo, unsigned int chipId, void **recvBuf,
    unsigned int *recvLen)
{
    struct drvHdcMsg *msgRcv = NULL;
    void *rcvBuf = NULL;
    int rcvLen = 0;
    int ret;

    ret = RA_HDC_OPS.allocMsg(asyncInfo->hdcSession, &msgRcv, 1);
    CHK_PRT_RETURN(ret != 0, hccp_err("alloc hdc msg failed ret %d", ret), ret);

    ret = RA_HDC_OPS.recv(asyncInfo->hdcSession, msgRcv, MAX_HDC_DATA, 0, &rcvLen, RA_HDC_RECV_SEND_TIMEOUT);
    if (ret != 0) {
        hccp_warn("recv hdc msg unsuccessful ret %d", ret);
        goto out;
    }

    RA_HDC_OPS.getMsgBuffer(msgRcv, 0, (char **)&rcvBuf, &rcvLen);
    if (rcvBuf == NULL || rcvLen == 0) {
        hccp_warn("get_msg_buffer unsuccessful, rcvBuf is NULL or rcvLen:%d is 0", rcvLen);
        goto out;
    }

    *recvBuf = (char *)calloc(rcvLen, sizeof(char));
    if (*recvBuf == NULL) {
        hccp_err("calloc recv_buf failed, errno:%d rcvLen:%d", errno, rcvLen);
        ret = -ENOMEM;
        goto out;
    }

    (void)memcpy_s(*recvBuf, rcvLen, rcvBuf, rcvLen);
    *recvLen = rcvLen;

out:
    RA_HDC_OPS.freeMsg(msgRcv);
    return ret;
}

int RaHdcAsyncSendPkt(struct RaHdcAsyncInfo *asyncInfo, unsigned int chipId, void *sendBuf,
    unsigned int sendLen)
{
    struct drvHdcMsg *msgSnd = NULL;
    int ret = -EINVAL;

    RA_PTHREAD_MUTEX_LOCK(&asyncInfo->sendMutex);
    // degrade log level because session will be closed by recv thread and request will be abort
    if (asyncInfo->hdcSession == NULL) {
        hccp_warn("[async][send_pkt]hdc_session is NULL, chipId(%u)", chipId);
        goto alloc_msg_err;
    }

    ret = RA_HDC_OPS.allocMsg(asyncInfo->hdcSession, &msgSnd, 1);
    if (ret != 0) {
        hccp_err("[async][send_pkt]HDC alloc msg err ret(%d) chip_id(%u)", ret, chipId);
        goto alloc_msg_err;
    }

    ret = RA_HDC_OPS.addMsgBuffer(msgSnd, sendBuf, sendLen);
    if (ret != 0) {
        hccp_err("[async][send_pkt]HDC add msg buffer err ret(%d) chip_id(%u)", ret, chipId);
        goto msg_err;
    }

    ret = RA_HDC_OPS.send(asyncInfo->hdcSession, msgSnd, RA_HDC_WAIT_TIMEOUT, RA_HDC_RECV_SEND_TIMEOUT);
    if (ret != 0) {
        hccp_err("[async][send_pkt]HDC send err ret(%d) chip_id(%u)", ret, chipId);
        goto msg_err;
    }

msg_err:
    RA_HDC_OPS.freeMsg(msgSnd);
alloc_msg_err:
    RA_PTHREAD_MUTEX_UNLOCK(&asyncInfo->sendMutex);
    return ret;
}

void RaHdcCloseSession(HDC_SESSION *session)
{
    RA_HDC_OPS.sessionClose(*session);
    *session = NULL;
    return;
}

STATIC void RaHwHdcInit(void *arg)
{
    unsigned int chipId = gHdcInitPara.chipId;
    pthread_t tidp;
    int ret;

    ret = pthread_detach(pthread_self());
    if (ret) {
        hccp_err("pthread detach failed ret %d", ret);
        return;
    }

    (void)prctl(PR_SET_NAME, (unsigned long)"hccp_hw_hdc");

    hccp_info("chip_id(%u)", chipId);
    gHdcInitPara.hdcFlag = 1;

    ret = pthread_create(&tidp, NULL, (void *)RaPthread, NULL);
    if (ret) {
        hccp_err("Create pthread failed, chipId(%u), ret(%d) ", chipId, ret);
        return;
    }

    while (1) {
        if (gHdcInitPara.connectStatus != HDC_UNCONNECTED) {
            usleep(HDC_ACCEPT_SLEEP_TIME);
            continue;
        }
        RaHwHdcCloseSession(&gHdcServer[chipId].hdcSession);
        ret = RaHdcSessionAccept(chipId, &gHdcServer[chipId].hdcSession, (int)gHdcInitPara.hostTgid);
        if (ret != 0) {
            hccp_warn("Session Accept unsuccessful, chipId(%u), ret(%d) ", chipId, ret);
            gHdcInitPara.hdcFlag = 0;
            return;
        }
        // original case, should continue to accept: host_tgid != g_hdc_init_para.host_tgid
        if (ret == 0 && gHdcServer[chipId].hdcSession == NULL) {
            continue;
        }
        RA_PTHREAD_MUTEX_LOCK(&gHdcInitPara.mutex);
        gHdcInitPara.connectStatus = HDC_CONNECTED;
        RA_PTHREAD_MUTEX_UNLOCK(&gHdcInitPara.mutex);
    }
}

STATIC void RaHwHdcDeinit(void)
{
    unsigned int chipId = gHdcInitPara.chipId;
    int ret, tryAgain;

    RA_PTHREAD_MUTEX_LOCK(&gHdcInitPara.mutex);
    gHdcInitPara.threadStatus = THREAD_DESTROYING;
    RA_PTHREAD_MUTEX_UNLOCK(&gHdcInitPara.mutex);

    tryAgain = HDC_TRY_TIME;
    while ((gHdcInitPara.threadStatus != THREAD_HALT) && tryAgain != 0) {
        usleep(HDC_USLEEP_TIME);
        tryAgain--;
    }
    if (tryAgain == 0) {
        hccp_warn("hdc message thread quit timeout, chipId:%u", chipId);
    }

    if (gHdcServer[chipId].hdcServer != NULL) {
        ret = RA_HDC_OPS.serverDestroy(gHdcServer[chipId].hdcServer);
        if (ret != 0) {
            hccp_warn("RA_HDC_OPS.server_destroy unsuccessful, ret:%d, chipId:%u", ret, chipId);
        }
        gHdcServer[chipId].hdcServer = NULL;
    } else {
        hccp_warn("hdc_server is NULL, chipId:%u", chipId);
    }
    pthread_mutex_destroy(&gHdcInitPara.mutex);
}

STATIC int HccpSetAffinity(unsigned int chipId)
{
    int ret;
    int64_t cpuId;
    int64_t ccpuNum; /* ctrl cpu */
    int64_t dcpuNum; /* data cpu */
    int64_t acpuNum; /* ai cpu */
    int64_t cpuCoreNum;
    cpu_set_t mask;

    ret = DlHalGetDeviceInfo(chipId, MODULE_TYPE_CCPU, INFO_TYPE_CORE_NUM, &ccpuNum);
    CHK_PRT_RETURN(ret, hccp_err("get ccpu_num failed, ret(%d)", ret), ret);

    ret = DlHalGetDeviceInfo(chipId, MODULE_TYPE_DCPU, INFO_TYPE_CORE_NUM, &dcpuNum);
    CHK_PRT_RETURN(ret, hccp_err("get dcpu_num failed, ret(%d)", ret), ret);

    ret = DlHalGetDeviceInfo(chipId, MODULE_TYPE_AICPU, INFO_TYPE_CORE_NUM, &acpuNum);
    CHK_PRT_RETURN(ret, hccp_err("get acpu_num failed, ret(%d)", ret), ret);
    cpuCoreNum = ccpuNum + dcpuNum + acpuNum;

    CPU_ZERO(&mask);
    cpuId = cpuCoreNum * chipId + HCCP_RUN_CPU_CORE;
    /*lint -e574*/
    CPU_SET((size_t)cpuId, &mask);  //lint !e573
    /*lint +e574*/
    hccp_run_info("chip_id:%u ccpu_num:%lld, dcpuNum:%lld, acpuNum:%lld, cpuId:%lld",
        chipId, ccpuNum, dcpuNum, acpuNum, cpuId);
    ret = sched_setaffinity(getpid(), sizeof(mask), &mask); /* hccp use core0 of each chip to setaffinity */
    CHK_PRT_RETURN(ret == -1, hccp_err("sched_setaffinity failed: ret %d, errno %d ", ret, errno), -ESYSFUNC);

    return 0;
}

STATIC int RaHwInit(unsigned int chipId, pid_t pid)
{
    int ret;
    pthread_t tidp;
    int timeout = RA_THREAD_TRY_TIME;

    gHdcInitPara.chipId = chipId;
    gHdcInitPara.hostTgid = pid;

    ret = pthread_create(&tidp, NULL, (void *)RaHwHdcInit, NULL);
    CHK_PRT_RETURN(ret, hccp_err("Create pthread failed, ret(%d) ", ret), -ESYSFUNC);

    while (gHdcInitPara.hdcFlag != 1 && timeout > 0) {
        usleep(RA_THREAD_SLEEP_TIME);
        timeout--;
    }

    CHK_PRT_RETURN(gHdcInitPara.hdcFlag == 0 || timeout == 0, hccp_err("HDC server thread create timeout,"
        "flag %d, timeout %d", gHdcInitPara.hdcFlag, timeout), -ESRCH);

    return 0;
}

RA_ADP_ATTRI_VISI_DEF int HccpInit(unsigned int chipId, pid_t pid, int hdcType, unsigned int whiteListStatus)
{
    struct timeval start, end;
    float timeCost = 0.0;
    int ret, retTmp;

    hccp_info("hccp[%u] hdc_type[%d] white_list_status[%u] init start", chipId, hdcType, whiteListStatus);

    ret = DlHalInit();
    if (ret != 0) {
        hccp_err("dl_hal_init failed, ret = %d", ret);
        return ret;
    }

    ret = HccpSetAffinity(chipId);
    if (ret != 0) {
        hccp_err("hccp_set_affinity failed, ret(%d) ", ret);
        goto out;
    }

    RsGetCurTime(&start);
    ret = RaHdcServerInit(chipId, hdcType);
    if (ret != 0) {
        hccp_err("chip_id[%u] hdc_type[%d] ra_hdc_server_init failed, ret[%d] ", chipId, hdcType, ret);
        goto out;
    }

    ret = pthread_mutex_init(&gHdcInitPara.mutex, NULL);
    if (ret != 0) {
        hccp_err("g_hdc_init_para mutex_init failed ret %d!, normal ret 0", ret);
        ret = -ESYSFUNC;
        goto out;
    }

    ret = RaHwInit(chipId, pid);
    if (ret != 0) {
        hccp_err("ra_init failed, ret(%d) ", ret);
        goto hw_init_err;
    }

    ret = RaHwAsyncInit(chipId, pid);
    if (ret != 0) {
        hccp_err("ra_hw_async_init failed, ret(%d) ", ret);
        goto hw_init_err;
    }

    RsGetCurTime(&end);
    HccpTimeInterval(&end, &start, &timeCost);
    hccp_info("ra_hw_init ok cost [%f] ms", timeCost);

    struct RsInitConfig offlineConfig = {
        .chipId = chipId,
        .hccpMode = NETWORK_OFFLINE,
        .whiteListStatus = whiteListStatus,
    };

    RsGetCurTime(&start);
    ret = RsInit(&offlineConfig);
    if (ret != 0) {
        hccp_err("rs_init failed (0x%x) ", ret);
        goto init_err;
    }
    RsGetCurTime(&end);
    HccpTimeInterval(&end, &start, &timeCost);
    hccp_info("rs_init ok cost [%f] ms", timeCost);

    RsGetCurTime(&start);
    ret = RsBindHostpid(chipId, pid);
    if (ret != 0) {
        hccp_err("rs_bind_hostpid failed, ret=%d ", ret);
        goto bind_hostpid_err;
    }
    RsGetCurTime(&end);
    HccpTimeInterval(&end, &start, &timeCost);
    hccp_info("rs_bind_hostpid ok cost [%f] ms", timeCost);

    RsGetCurTime(&start);
    ret = RsPingHandleInit(chipId, hdcType, whiteListStatus);
    if (ret != 0) {
        hccp_err("rs_ping_handle_init failed, ret=%d ", ret);
        goto bind_hostpid_err;
    }
    RsGetCurTime(&end);
    HccpTimeInterval(&end, &start, &timeCost);
    hccp_info("rs_ping_handle_init ok cost [%f] ms", timeCost);

    return 0;
bind_hostpid_err:
    retTmp = RsDeinit(&offlineConfig);
    if (retTmp) {
        hccp_err("rs_deinit failed %d ", retTmp);
    }
init_err:
    RaHwAsyncDeinit();
hw_init_err:
    pthread_mutex_destroy(&gHdcInitPara.mutex);
out:
    DlHalDeinit();
    return ret;
}

RA_ADP_ATTRI_VISI_DEF int HccpDeinit(unsigned int chipId)
{
    struct RsInitConfig offlineConfig = {
        .chipId = chipId,
        .hccpMode = NETWORK_OFFLINE,
        .whiteListStatus = WHITE_LIST_ENABLE,
    };
    int ret;

    hccp_info("hccp[%u] deinit start", chipId);

    ret = RsPingHandleDeinit(chipId);
    CHK_PRT_RETURN(ret, hccp_err("rs_ping_handle_deinit failed %d ", ret), ret);

    ret = RsDeinit(&offlineConfig);
    CHK_PRT_RETURN(ret, hccp_err("rs_deinit failed %d ", ret), ret);

    RaHwHdcDeinit();

    RaHwAsyncDeinit();
    DlHalDeinit();
    hccp_info("hccp [%u] deinit success", chipId);

    return ret;
}
